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