|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const shell = require('shelljs'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const { run, execSilent } = require('./util'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Script for setting up the library in another local project |
| 10 | + * |
| 11 | + * ### Usage |
| 12 | + * |
| 13 | + * 1) `./localInstall install /path/to/target` |
| 14 | + * |
| 15 | + * Generate and install a tarball package to a target npm module. Useful for locally |
| 16 | + * testing a build that will be sent to NPM. Accepts a relative or absolute path. |
| 17 | + * |
| 18 | + * 2) `./localInstall.js link /path/to/target`. |
| 19 | + * |
| 20 | + * Link the library to another project for development purposes. Changes made in this |
| 21 | + * project will be automatically reflected in the target module. |
| 22 | + * |
| 23 | + * 3) `./localInstall.js unlink /path/to/target` |
| 24 | + * |
| 25 | + * Unlink the library from another project. |
| 26 | + */ |
| 27 | + |
| 28 | +const COMMANDS = ['install', 'link', 'unlink']; |
| 29 | + |
| 30 | +function showHelp() { |
| 31 | + console.log('Commands:'); |
| 32 | + console.log( |
| 33 | + ' install [path to target module]\tCreates an NPM package of the project and installs it to the target module' |
| 34 | + ); |
| 35 | + console.log(' link [path to target module]\t\tLink the project to another module for quick development'); |
| 36 | + console.log(' unlink [path to target module]\tUnlink the project from the target module'); |
| 37 | +} |
| 38 | + |
| 39 | +function main() { |
| 40 | + const command = process.argv[2]; |
| 41 | + if (!COMMANDS.includes(command)) { |
| 42 | + showHelp(); |
| 43 | + process.exit(0); |
| 44 | + } |
| 45 | + |
| 46 | + const targetPackagePath = !path.isAbsolute(process.argv[3]) |
| 47 | + ? path.resolve(process.cwd(), process.argv[3]) |
| 48 | + : process.argv[3]; |
| 49 | + |
| 50 | + if (!fs.existsSync(targetPackagePath)) { |
| 51 | + console.log(`A valid target package path is required`); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | + |
| 55 | + const isDirectory = fs.lstatSync(targetPackagePath).isDirectory(); |
| 56 | + const packageJsonPath = path.join(targetPackagePath, 'package.json'); |
| 57 | + if (!isDirectory || !fs.existsSync(packageJsonPath)) { |
| 58 | + console.log('Path must be to a valid npm package'); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + |
| 62 | + const localPackagePath = path.join(__dirname, '..'); |
| 63 | + const { name, version } = JSON.parse(fs.readFileSync(path.join(localPackagePath, 'package.json')).toString()); |
| 64 | + |
| 65 | + switch (command) { |
| 66 | + case COMMANDS[0]: // install |
| 67 | + let tarballPath; |
| 68 | + run('Building project and creating package', () => { |
| 69 | + shell.cd(localPackagePath); |
| 70 | + execSilent('yarn build'); |
| 71 | + execSilent('yarn pack'); |
| 72 | + tarballPath = execSilent('find $(pwd) -type f -iname *.tgz').replace('\n', ''); |
| 73 | + }); |
| 74 | + |
| 75 | + run(`Installing v${version} to ${targetPackagePath}`, () => { |
| 76 | + shell.cd(targetPackagePath); |
| 77 | + const yarnLockPath = path.join(targetPackagePath, 'yarn.lock'); |
| 78 | + if (fs.existsSync(yarnLockPath)) { |
| 79 | + execSilent(`yarn remove ${name}`, true); |
| 80 | + execSilent(`yarn cache clean`); |
| 81 | + execSilent(`yarn add ${tarballPath}`); |
| 82 | + } else { |
| 83 | + execSilent(`npm uninstall ${name}`); |
| 84 | + execSilent(`npm install ${tarballPath}`); |
| 85 | + } |
| 86 | + }); |
| 87 | + break; |
| 88 | + case COMMANDS[1]: // link |
| 89 | + run(`Linking ${name} to ${targetPackagePath}`, () => { |
| 90 | + shell.cd(localPackagePath); |
| 91 | + execSilent('yarn link'); |
| 92 | + shell.cd(targetPackagePath); |
| 93 | + execSilent(`yarn link ${name}`); |
| 94 | + }); |
| 95 | + break; |
| 96 | + case COMMANDS[2]: // unlink |
| 97 | + run(`Unlinking ${name} from ${targetPackagePath}`, () => { |
| 98 | + shell.cd(targetPackagePath); |
| 99 | + execSilent(`yarn unlink ${name}`); |
| 100 | + shell.cd(localPackagePath); |
| 101 | + execSilent('yarn unlink'); |
| 102 | + }); |
| 103 | + break; |
| 104 | + default: |
| 105 | + showHelp(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +main(); |
0 commit comments