-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.ts
107 lines (87 loc) · 2.88 KB
/
bin.ts
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
#!/usr/bin/env node
import { spawnSync } from 'child_process';
import glob from 'fast-glob';
import findUp from 'find-up';
import { access, ensureDir, copy, mkdtemp } from 'fs-extra';
import { tmpdir } from 'os';
import { dirname, join } from 'path';
function tryRequire(module: string): any | null {
try {
return require(module);
} catch (_) {
return null;
}
}
async function tryAccess(path: string): Promise<boolean> {
try {
return (await access(path)) === undefined;
} catch (_) {
return false;
}
}
async function globPackages(
cwd: string,
patterns: string[]
): Promise<string[]> {
const manifestPatterns = patterns.map((p: string) => join(p, 'package.json'));
const packages = await glob(manifestPatterns, { cwd });
return packages.map((p: string) => dirname(p));
}
const nothing = Promise.resolve();
async function main(): Promise<void> {
const gitDir = await findUp('.git');
if (!gitDir) {
throw new Error(
`Could not find a '.git' repository upwards from ${process.cwd()}`
);
}
const root = dirname(gitDir);
const manifest = require(join(root, 'package.json'));
const lerna = tryRequire(join(root, 'lerna.json'));
const packagePatterns = manifest.workspaces || (lerna ? lerna.packages : []);
const [hasNpmLock, hasYarnLock, hasGitIgnore, packages] = await Promise.all([
tryAccess(join(root, 'package-lock.json')),
tryAccess(join(root, 'yarn.lock')),
tryAccess(join(root, '.gitignore')),
globPackages(root, packagePatterns)
]);
if (!(hasNpmLock || hasYarnLock)) {
throw new Error(
`Could not find either a 'yarn.lock' or 'package-lock.json - aborted.`
);
}
const tmp = await mkdtemp(join(tmpdir(), manifest.name.replace('/', '-')));
process.chdir(tmp);
const copyToTmp = (name: string): Promise<void> =>
copy(join(root, name), join(tmp, name));
await Promise.all([
copyToTmp('package.json'),
hasGitIgnore ? copyToTmp('.gitignore') : nothing,
hasYarnLock ? copyToTmp('yarn.lock') : nothing,
hasNpmLock ? copyToTmp('package-lock.json') : nothing,
...packages.map(async p => {
await ensureDir(join(tmp, p));
return copyToTmp(join(p, 'package.json'));
})
]);
spawnSync('git', ['init'], { stdio: 'inherit' });
spawnSync(hasYarnLock ? 'yarn' : 'npm', ['install', '--ignore-scripts'], {
stdio: ['inherit', 'inherit', 'ignore']
});
spawnSync('git', ['add', '-f', '.', 'node_modules', ...packagePatterns], {
stdio: 'ignore'
});
spawnSync('git', ['commit', '--no-verify', '-m', 'initial'], {
stdio: 'ignore'
});
spawnSync('cp', ['-r', join(root, 'node_modules'), tmp]);
packages.forEach(p =>
spawnSync('cp', ['-r', join(root, p, 'node_modules'), join(tmp, p)])
);
spawnSync(
'git',
['diff', '--', '.', ':(exclude)node_modules/.yarn-integrity'],
{ stdio: ['inherit', 'inherit', 'ignore'] }
);
}
main().catch(console.error);