/
index.js
175 lines (142 loc) · 4.8 KB
/
index.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
172
173
174
175
#!/usr/bin/env node
const path = require('path');
const { exec } = require('child_process');
const { promisify } = require('util');
const execPromise = promisify(exec);
const fs = require('fs-extra');
const colors = require('colors');
const parseArgs = require('minimist');
const hasBinary = require('./src/has-binary');
const diffDirectories = require('./src/gen-diff-object');
const isInvalidPath = require('./src/is-invalid-path');
const { version } = require('./package.json');
(async () => {
const has_zip = hasBinary('zip');
const has_diff = hasBinary('diff');
if (!(has_zip && has_diff)) {
// prettier-ignore
console.error(`Could not find both "${'zip'.yellow}" and "${'diff'.yellow}" binaries, both of which are required. Exiting.`);
process.exit(1);
}
const arg_options = {
boolean: ['quiet', 'json'],
string: ['output'],
default: {
quiet: false,
json: false,
output: 'build_for_upload',
},
alias: {
q: 'quiet',
j: 'json',
o: 'output',
},
};
const argv = parseArgs(process.argv.slice(2), arg_options);
// Minimst uses an underscore (`_` for non-keyed arguments)
let [build_old, build_new] = argv._;
// @todo Have options table automatically generated
const usage_message = `
Usage: build-diff [options] <old-build-directory> <new-build-directory>
CLI to compare two folders and copy out the differences between them.
Version ${version}
Options:
-q, --quiet Hides progress as it compares the directories. Defaults to false.
-j, --json Outputs results as JSON. Defaults to false.
-o, --output <name> Name of the output folder and ZIP file. Defaults to "build_for_upload".
`;
if (!build_old || !build_new) {
console.error(usage_message);
process.exit(1);
}
// Check that build folders exist
let build_old_exists = fs.existsSync(build_old);
let build_new_exists = fs.existsSync(build_new);
if (!build_old_exists || !build_new_exists) {
console.error(
'The passed build directories do not exist:'.red,
`\n "${build_old}"\n "${build_new}"\n`
);
console.error(usage_message);
process.exit(1);
}
// Destructure flags
const { quiet, json, output } = argv;
if (isInvalidPath(output)) {
console.error(
'The passed output directory has invalid characters:'.red,
`\n "${output}"\n`
);
console.error(usage_message);
process.exit(1);
}
!quiet && console.log(`Comparing "${build_old.magenta}" against "${build_new.magenta}"...`);
// If we are here, both directories exist, so let's compute the differences between them
let {
filesAdded: files_added,
filesUpdated: files_updated,
filesDeleted: files_deleted,
} = await diffDirectories(build_old, build_new, { quiet });
if (files_added.length + files_updated + files_deleted.length === 0) {
console.error(`No files were different between "${build_old}" and "${build_new}", exiting`);
process.exit(0);
}
// Concat 'added' and 'updated' since functionally they are no different when uploading to AWS (or other)
let files_changed = files_updated.concat(files_added);
// Sort newly formed `files_changed`; other lists are already sorted
files_changed.sort();
let output_dir = path.resolve(output);
// Create our output directory
try {
await fs.ensureDir(output_dir);
} catch (e) {
console.error(`Unable to create output directory at "${output_dir}"`);
console.error(e);
process.exit(2);
}
// Copy our changed files over to the output directory!
if (files_changed.length) {
!quiet && process.stdout.write('Copying over changed files... '.yellow);
await Promise.all(
files_changed.map((file) => {
return new Promise((resolve, reject) => {
fs.copy(`${build_new}${path.sep}${file}`, `${output_dir}${path.sep}${file}`)
.then(() => resolve())
.catch(() => reject());
});
})
);
!quiet && console.log('Done'.green);
}
// Zip the files up
// @todo Use a native option
!quiet && process.stdout.write('Zipping changed files... '.yellow);
let relative_output_dir = path.relative(process.cwd(), output_dir);
await execPromise(
`zip -9 -x "**/.DS_Store" -q -r ${relative_output_dir}.zip ${relative_output_dir}`
);
!quiet && console.log('Done\n'.green);
if (json) {
const json_output = {
filesDeleted: files_deleted,
filesChanged: files_changed,
outputDir: relative_output_dir,
outputZip: relative_output_dir + '.zip',
};
console.log(JSON.stringify(json_output));
} else {
if (files_deleted.length) {
console.log('The following files were deleted:'.red);
console.log(' ' + files_deleted.join('\n ') + '\n');
}
if (files_changed.length) {
console.log('\nThe following files were changed:'.green);
console.log(' ' + files_changed.join('\n ') + '\n');
}
console.log(
`All changed files have been copied to ${relative_output_dir.cyan}, and zipped in ${
(relative_output_dir + '.zip').cyan
}\n`
);
}
})();