forked from jupyterlab/jupyterlab
/
clean-packages.ts
60 lines (56 loc) · 1.76 KB
/
clean-packages.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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import * as fs from 'fs-extra';
import * as path from 'path';
import * as glob from 'glob';
import { readJSONFile } from './utils';
// Get all of the packages.
let basePath = path.resolve('.');
let baseConfig = readJSONFile(path.join(basePath, 'package.json'));
let packageConfig = baseConfig.workspaces;
let skipSource = process.argv.indexOf('packages') === -1;
let skipExamples = process.argv.indexOf('examples') === -1;
// Handle the packages
for (let i = 0; i < packageConfig.length; i++) {
if (skipSource && packageConfig[i] === 'packages/*') {
continue;
}
if (skipExamples && packageConfig[i] === 'examples/*') {
continue;
}
let files = glob.sync(path.join(basePath, packageConfig[i]));
for (let j = 0; j < files.length; j++) {
try {
handlePackage(files[j]);
} catch (e) {
console.error(e);
}
}
}
/**
* Handle an individual package on the path - update the dependency.
*/
function handlePackage(packagePath: string): void {
// Read in the package.json.
let packageJSONPath = path.join(packagePath, 'package.json');
let data: any;
try {
data = require(packageJSONPath);
} catch (e) {
console.log('skipping', packagePath);
return;
}
if (!data.scripts || !data.scripts.clean) {
return;
}
let targets = data.scripts.clean.split('&&');
for (let i = 0; i < targets.length; i++) {
let target = targets[i].replace('rimraf', '').trim();
target = path.join(packagePath, target);
if (fs.existsSync(target)) {
fs.removeSync(target);
}
}
}