forked from graphql/graphiql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenameFileExtensions.js
51 lines (45 loc) · 1.43 KB
/
renameFileExtensions.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
const copy = require('copy');
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const crypto = require('crypto');
const rimraf = require('rimraf');
const [, , src, dest, destExtension] = process.argv;
if (!src || !dest || !destExtension) {
console.error(
`\nMissing arguments.\n\nUsage:\nnode renameFileExtensions.js './dist/**/*.js' './dest-dir' .new.extension.js`,
);
process.exit(1);
}
const coveragePath = path.join(__dirname, `../coverage`);
const tempRenamePath = path.join(
coveragePath,
'.temp',
crypto.randomBytes(20).toString('hex'),
);
if (fs.existsSync(tempRenamePath)) {
rimraf.sync(tempRenamePath);
}
const tempPath = mkdirp.sync(tempRenamePath);
if (tempPath) {
copy(src, tempRenamePath, (error, files) => {
if (error) {
throw error;
}
files.forEach(file => {
if (file.dest) {
const srcExt = path.parse(file.dest).ext;
const destinationPath = file.dest
.replace(srcExt, destExtension) // rewrite extension
.replace(tempRenamePath, dest); // and destination path
// move the files and rename them... by renaming them :)
fs.renameSync(file.dest, path.resolve(destinationPath));
}
});
// should cleanup temp directory after renaming
// every file to the destination path
rimraf.sync(tempRenamePath);
});
} else {
throw Error(`Could not generate temporary path\n${tempRenamePath}`);
}