forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrub.js
171 lines (152 loc) · 5.13 KB
/
scrub.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// @ts-check
const child_process = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
// This script MUST NOT have any deps aside from Node built-ins, because it deletes all node_modules!
const verbose = process.argv.includes('--verbose') || process.argv.includes('-v');
/**
* @param {string} question - question to ask the user
* @returns {Promise<string>} response
*/
function prompt(question) {
return new Promise(resolve => {
process.stdin.resume();
process.stdout.write(question);
process.stdin.once('data', data => {
resolve(data.toString().trim());
process.stdin.pause();
});
});
}
/**
* @param {string} itemPath
* @param {string[]} failedPaths
*/
function deleteIfSymlink(itemPath, failedPaths) {
try {
// Compare realpath since fs.statSync(itemPath).isSymbolicLink() doesn't work on Windows
if (fs.existsSync(itemPath) && fs.realpathSync(itemPath) !== itemPath) {
if (verbose) {
console.log(' Deleting symlink: ' + itemPath);
}
fs.unlinkSync(itemPath);
}
} catch (ex) {
console.warn(`Error running realpath or unlink on ${itemPath}: ${ex}`);
failedPaths.push(itemPath);
}
}
/**
* Delete symlinks from a package's node_modules folder
* @param {string} packagePath
* @param {string[]} failedPaths
*/
function deleteNodeModulesSymlinks(packagePath, failedPaths) {
const nodeModulesPath = path.resolve(packagePath, 'node_modules');
if (!fs.existsSync(nodeModulesPath)) {
return;
}
// Check node_modules for symlinks and manually remove those
// (using this odd way of iterating since we're adding more modules to the list as we go)
const modules = fs.readdirSync(nodeModulesPath);
/** @type {string} */
let mod;
while ((mod = modules.pop())) {
const modulePath = path.join(nodeModulesPath, mod);
if (mod[0] === '@' && !/[/\\]/.test(mod)) {
// Add any scoped modules to the list of things to check
modules.push(...fs.readdirSync(modulePath).map(m => path.join(mod, m)));
} else {
deleteIfSymlink(modulePath, failedPaths);
}
}
}
/**
* @param {string} parentFolder
*/
function getChildren(parentFolder) {
return fs.readdirSync(parentFolder).map(child => path.join(parentFolder, child));
}
/**
* @param {string} cmd
* @param {string[]} args
*/
function spawn(cmd, args) {
const maxBuffer = 1024 * 1024 * 10; // default is 1024 * 1024
const result = child_process.spawnSync(cmd, args, { stdio: 'inherit', maxBuffer });
if (result.error) {
throw result.error;
} else if (result.status) {
throw new Error('Command failed');
}
}
async function run() {
if (!fs.existsSync(path.join(process.cwd(), '.git'))) {
console.error('Please run this script from the root of the Git repo');
process.exit(1);
}
const gitStatus = child_process.execSync('git status --porcelain').toString().trim();
if (!process.argv.includes('-y')) {
console.log(
'WARNING: This command will PERMANENTLY DELETE all untracked files (such as build output and node_modules).',
);
if (gitStatus) {
console.log('It will also revert uncommitted changes to the following files:');
const lines = gitStatus.split(/\r?\n/g).map(line => ' ' + line);
const showFileCount = 20;
console.log(lines.slice(0, showFileCount).join('\n'));
if (lines.length > showFileCount) {
console.log(` ...and ${lines.length - showFileCount} more`);
}
}
const answer = await prompt('Are you sure you want to proceed? (yes/no) ');
if (answer.toLowerCase()[0] !== 'y') {
return;
}
}
// do these before deleting node_nodules
console.log('\nClearing Jest cache...');
try {
spawn(os.platform() === 'win32' ? 'npx.cmd' : 'npx', ['jest', '--clearCache']);
} catch (err) {
console.error('Clearing jest cache failed, likely due to it or a dep not being installed.');
}
try {
console.log('\nAttempting to clear gulp-cache...');
const cache = require('gulp-cache');
cache.clearAll();
console.log('...success!');
} catch (err) {
console.log('Clearing gulp-cache failed, likely due it not being installed.');
}
const failedPaths = [];
console.log("\nDeleting symlinks from packages' node_modules and rush temp files...");
const folders = [
'.',
...getChildren('apps'),
...getChildren('packages'),
...getChildren('packages/fluentui'),
'scripts',
'common/temp',
];
for (const folder of folders) {
deleteNodeModulesSymlinks(folder, failedPaths);
}
deleteIfSymlink(path.resolve('common/temp/pnpm-local'), failedPaths);
if (failedPaths.length) {
console.error('Deleting the following symlinks failed. Please delete these manually and try again.');
console.error(failedPaths.map(p => ' ' + p).join('\n'));
process.exit(1);
}
console.log('\nRunning "git clean -fdx" to remove all untracked files/folders (this may take awhile)...');
spawn('git', ['clean', '-fdx']);
console.log('\nRunning "git reset --hard"...');
spawn('git', ['reset', '--hard']);
console.log('\nDone!');
}
run().catch(ex => {
console.error('Caught error:');
console.error(ex);
process.exit(1);
});