diff --git a/README.md b/README.md index 0e31c5c..ec7a196 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/bin/index.mjs b/bin/index.mjs index 54407a6..834261f 100755 --- a/bin/index.mjs +++ b/bin/index.mjs @@ -4,7 +4,7 @@ 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'; @@ -12,10 +12,11 @@ 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 ', 'Install a package from the local registry', {}, (argv) => install(argv.packageName)) .demandCommand(1, chalk.red('You need to specify a command')) .strict() .help() diff --git a/commands/install.mjs b/commands/install.mjs index ed6db48..b8dc5b1 100644 --- a/commands/install.mjs +++ b/commands/install.mjs @@ -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!')); }; diff --git a/commands/publish.mjs b/commands/publish.mjs index f5f6995..6bbbd66 100644 --- a/commands/publish.mjs +++ b/commands/publish.mjs @@ -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!')); }; diff --git a/config/paths.mjs b/config/paths.mjs index 0b3a7be..bbab2e5 100644 --- a/config/paths.mjs +++ b/config/paths.mjs @@ -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'); diff --git a/package.json b/package.json index 3b562c7..444f792 100644 --- a/package.json +++ b/package.json @@ -33,4 +33,4 @@ "npm": ">=7.0.0", "yarn": ">=1.0.0" } -} \ No newline at end of file +}