forked from adobe/react-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildIcons.js
100 lines (91 loc) · 2.91 KB
/
buildIcons.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
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import spawn from 'cross-spawn';
import glob from 'fast-glob';
import {promises as fs} from 'fs';
import path from 'path';
const PACKAGES = {
ui: path.dirname(require.resolve('@adobe/react-spectrum-ui/dist/')),
workflow: path.dirname(
require.resolve('@adobe/react-spectrum-workflow/dist/')
),
color: path.dirname(
require.resolve('@adobe/react-spectrum-workflow-color/dist/')
),
illustrations: path.join(getIconPackageFolder('illustrations'), 'src')
};
(async function () {
// run in packages where at least one dist js file is newer that the corresponding source js file
for (let [pkg, srcFolder] of Object.entries(PACKAGES)) {
let distFolder = getIconPackageFolder(pkg);
for (let srcFile of await glob([
path.join(srcFolder, '*.{js,tsx}').replace(/\\/g, '/')
])) {
let filename = path.basename(srcFile);
if (filename === 'index.js' || filename === 'util.js') {
continue;
}
let distFile =
path.join(distFolder, path.basename(srcFile, path.extname(srcFile))) +
'.js';
if (!(await exists(distFile)) || (await isNewerThan(srcFile, distFile))) {
console.log(`Building icons for @spectrum-icons/${pkg}`);
if (pkg !== 'illustrations') {
await run('yarn', ['make-icons'], {
cwd: distFolder,
stdio: 'inherit'
});
}
await run('yarn', ['build-icons'], {
cwd: distFolder,
stdio: 'inherit'
});
break;
}
}
}
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});
function run(cmd, args, opts) {
return new Promise((resolve, reject) => {
let child = spawn(cmd, args, opts);
child.on('error', reject);
child.on('close', (code) => {
if (code !== 0) {
reject(new Error('Child process failed'));
return;
}
resolve();
});
});
}
function getIconPackageFolder(name) {
return path.resolve(
path.join(__dirname, '..', 'packages', '@spectrum-icons', name)
);
}
async function isNewerThan(a, b) {
let a_mtime = (await fs.stat(a)).mtime;
let b_mtime = (await fs.stat(b)).mtime;
return a_mtime > b_mtime;
}
async function exists(f) {
try {
await fs.access(f);
return true;
} catch (e) {
if (e.code === 'ENOENT') return false;
else throw e;
}
}