Skip to content

Commit

Permalink
feat: use json to allow install package by name without conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
edumudu committed Mar 16, 2022
1 parent 07636d7 commit d6b7a73
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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.

Expand Down
3 changes: 2 additions & 1 deletion bin/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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', {}, () => publish())
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 d6b7a73

Please sign in to comment.