Skip to content

Commit

Permalink
Merge pull request #1 from edumudu/feat/improvements
Browse files Browse the repository at this point in the history
Genral improvements in the resolver
  • Loading branch information
edumudu committed Mar 16, 2022
2 parents adcf3e1 + d6b7a73 commit 36f6469
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ Enter in your pacakge directory and run
```bash
loctry publish
```
> NOTE: This command needs to be run in the same dir as the `package.json`
This command will pack and send your package tarball to the `.loctry` folder to be avaiable to be installed in others projects. To install the package enter in the project directory and run
This command will pack and send your package tarball to the `.loctry` folder to be avaiable to be installed in others projects.

> NOTE: Do not modify `.loctry` dir manually. This is an internal API
To install the package enter in the project directory and run
```bash
loctry install <package-name>
```
Expand Down
7 changes: 4 additions & 3 deletions bin/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import chalk from 'chalk';

import { modulesRoot, packageDir } from '../config/paths.mjs';
import { modulesRoot, packageDir, packagesInfoPath } from '../config/paths.mjs';

import publish from '../commands/publish.mjs';
import install from '../commands/install.mjs';

// Verify if global node_modules and package exists, if not, create it
if(!fs.existsSync(packageDir)) fs.mkdirSync(packageDir);
if(!fs.existsSync(modulesRoot)) fs.mkdirSync(modulesRoot);
if(!fs.existsSync(packagesInfoPath)) fs.writeFileSync(packagesInfoPath, '{}');

yargs(hideBin(process.argv))
.command('publish', 'Publish a package to the local registry', (yargsInstance) => publish(yargsInstance.parse()))
.command('install', 'Install a package from the local registry', (yargsInstance) => install(yargsInstance.parse()._[1]))
.command('publish', 'Publish a package to the local registry', {}, () => publish())
.command('install <packageName>', 'Install a package from the local registry', {}, (argv) => install(argv.packageName))
.demandCommand(1, chalk.red('You need to specify a command'))
.strict()
.help()
Expand Down
14 changes: 5 additions & 9 deletions commands/install.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';

import { modulesRoot } from '../config/paths.mjs';
import { packagesInfoPath } from '../config/paths.mjs';

const install = (packageName) => {
const filenameMatchRegex = new RegExp(`^${packageName}-v?\\d+\\.\\d+\\.\\d+\\.tgz$`);
const availableVersions = fs.readdirSync(modulesRoot).filter((file) => filenameMatchRegex.test(file));
const tarballFilename = availableVersions[availableVersions.length - 1];
const packagesInfo = JSON.parse(fs.readFileSync(packagesInfoPath, 'utf-8'));
const tarballPath = packagesInfo[packageName]?.tarballPath;

if(!tarballFilename) {
if(!tarballPath) {
console.log(chalk.red(`Package ${packageName} not found in the local registry`));
process.exit(1);
};

const packageTarballAbsPath = path.join(modulesRoot, tarballFilename);

execSync(`npm install ${packageTarballAbsPath}`);
execSync(`npm install ${tarballPath}`);
console.log(chalk.green('Package installed successfully!'));
};

Expand Down
17 changes: 15 additions & 2 deletions commands/publish.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { execSync } from 'child_process'
import chalk from 'chalk';
import fs from 'fs';
import { join } from 'path';

import { modulesRoot } from '../config/paths.mjs';
import { modulesRoot, packagesInfoPath } from '../config/paths.mjs';

const publish = () => {
execSync(`npm pack --pack-destination ${modulesRoot}`, { stdio: 'inherit' });
const packageInfo = JSON.parse(fs.readFileSync(join(process.cwd(), 'package.json')));
const packagesInfo = JSON.parse(fs.readFileSync(packagesInfoPath));

const tarballFilename = execSync(`npm pack --pack-destination ${modulesRoot}`).toString().trim();

packagesInfo[packageInfo.name] = {
name: packageInfo.name,
version: packageInfo.version,
description: packageInfo.description,
tarballPath: path.join(modulesRoot, tarballFilename),
};

fs.writeFileSync(packagesInfoPath, JSON.stringify(packagesInfo, null, 2));
console.log(chalk.green('Package published successfully!'));
};

Expand Down
1 change: 1 addition & 0 deletions config/paths.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import os from 'os';

export const packageDir = path.join(os.homedir(), '.loctry');
export const modulesRoot = path.join(packageDir, 'node_modules');
export const packagesInfoPath = path.join(packageDir, 'packages-info.json');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
"npm": ">=7.0.0",
"yarn": ">=1.0.0"
}
}
}

0 comments on commit 36f6469

Please sign in to comment.