-
Notifications
You must be signed in to change notification settings - Fork 3
/
collect.js
79 lines (58 loc) · 1.96 KB
/
collect.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
const { execSync } = require('child_process');
const { ncp } = require('ncp').ncp;
const fs = require('fs');
const path = require( 'path' );
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
console.log("Building Examples");
const source = `${__dirname}/examples/`;
const collectionFile = `${__dirname}/src/examples.json`;
const publicExamplesPath = `${__dirname}/public/examples/`;
rimraf(publicExamplesPath, () => {
fs.mkdirSync(publicExamplesPath);
build(source);
});
function build(source) {
//Reset collection file
fs.writeFileSync(collectionFile, JSON.stringify([]));
// Loop through all the files in the source directory
fs.readdir( source, function( err, files ) {
if (err) { throw err; }
files.forEach( function( name, index ) {
var examplePath = path.join( source, name );
fs.stat( examplePath, function( err, stat ) {
if (err) { throw err; }
if (stat.isDirectory()) {
buildExample(name);
}
});
});
});
}
function buildExample(name) {
console.info(`Building ${name}`);
const examplePath = `${__dirname}/examples/${name}`;
const publicPath = `${__dirname}/public/examples/${name}`;
execSync('yarn install', {cwd: examplePath});
execSync('yarn build', {cwd: examplePath});
let stat = fs.stat(publicPath, (err, stat) => {
if (err) {
fs.mkdirSync(publicPath);
}
ncp(`${examplePath}/build`, publicPath, function (err) {
if (err) { throw err; }
let packageJson = fs.readFileSync(`${examplePath}/package.json`);
let packageData = JSON.parse(packageJson);
let entry = {
"id": name,
"title": packageData.name,
"description": packageData.description,
}
let data = fs.readFileSync(collectionFile);
let entries = JSON.parse(data);
entries.push(entry);
fs.writeFileSync(collectionFile, JSON.stringify(entries));
console.log(`${name} done!`);
});
});
}