|
4 | 4 | const version = '1.9.1';
|
5 | 5 |
|
6 | 6 | (async () => {
|
| 7 | + const os = require('os'); |
| 8 | + const { promises: fs } = require('fs'); |
| 9 | + const path = require('path'); |
| 10 | + const shell = require('shelljs'); |
| 11 | + const { v4 } = require('uuid'); |
7 | 12 |
|
8 |
| - const os = require('os'); |
9 |
| - const path = require('path'); |
10 |
| - const shell = require('shelljs'); |
11 |
| - const { v4 } = require('uuid'); |
| 13 | + const repository = path.join(os.tmpdir(), `${v4()}-arduino-examples`); |
| 14 | + if (shell.mkdir('-p', repository).code !== 0) { |
| 15 | + shell.exit(1); |
| 16 | + } |
12 | 17 |
|
13 |
| - const repository = path.join(os.tmpdir(), `${v4()}-arduino-examples`); |
14 |
| - if (shell.mkdir('-p', repository).code !== 0) { |
15 |
| - shell.exit(1); |
16 |
| - process.exit(1); |
17 |
| - } |
18 |
| - |
19 |
| - if (shell.exec(`git clone https://github.com/arduino/arduino-examples.git ${repository}`).code !== 0) { |
20 |
| - shell.exit(1); |
21 |
| - process.exit(1); |
22 |
| - } |
| 18 | + if ( |
| 19 | + shell.exec( |
| 20 | + `git clone https://github.com/arduino/arduino-examples.git ${repository}` |
| 21 | + ).code !== 0 |
| 22 | + ) { |
| 23 | + shell.exit(1); |
| 24 | + } |
23 | 25 |
|
24 |
| - if (shell.exec(`git -C ${repository} checkout tags/${version} -b ${version}`).code !== 0) { |
25 |
| - shell.exit(1); |
26 |
| - process.exit(1); |
27 |
| - } |
| 26 | + if ( |
| 27 | + shell.exec(`git -C ${repository} checkout tags/${version} -b ${version}`) |
| 28 | + .code !== 0 |
| 29 | + ) { |
| 30 | + shell.exit(1); |
| 31 | + } |
28 | 32 |
|
29 |
| - const destination = path.join(__dirname, '..', 'Examples'); |
30 |
| - shell.mkdir('-p', destination); |
31 |
| - shell.cp('-fR', path.join(repository, 'examples', '*'), destination); |
| 33 | + const destination = path.join(__dirname, '..', 'Examples'); |
| 34 | + shell.mkdir('-p', destination); |
| 35 | + shell.cp('-fR', path.join(repository, 'examples', '*'), destination); |
32 | 36 |
|
| 37 | + const isSketch = async (pathLike) => { |
| 38 | + try { |
| 39 | + const names = await fs.readdir(pathLike); |
| 40 | + const dirName = path.basename(pathLike); |
| 41 | + return names.indexOf(`${dirName}.ino`) !== -1; |
| 42 | + } catch (e) { |
| 43 | + if (e.code === 'ENOTDIR') { |
| 44 | + return false; |
| 45 | + } |
| 46 | + throw e; |
| 47 | + } |
| 48 | + }; |
| 49 | + const examples = []; |
| 50 | + const categories = await fs.readdir(destination); |
| 51 | + const visit = async (pathLike, container) => { |
| 52 | + const stat = await fs.lstat(pathLike); |
| 53 | + if (stat.isDirectory()) { |
| 54 | + if (await isSketch(pathLike)) { |
| 55 | + container.sketches.push({ |
| 56 | + name: path.basename(pathLike), |
| 57 | + relativePath: path.relative(destination, pathLike), |
| 58 | + }); |
| 59 | + } else { |
| 60 | + const names = await fs.readdir(pathLike); |
| 61 | + for (const name of names) { |
| 62 | + const childPath = path.join(pathLike, name); |
| 63 | + if (await isSketch(childPath)) { |
| 64 | + container.sketches.push({ |
| 65 | + name, |
| 66 | + relativePath: path.relative(destination, childPath), |
| 67 | + }); |
| 68 | + } else { |
| 69 | + const child = { |
| 70 | + label: name, |
| 71 | + children: [], |
| 72 | + sketches: [], |
| 73 | + }; |
| 74 | + container.children.push(child); |
| 75 | + await visit(childPath, child); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + for (const category of categories) { |
| 82 | + const example = { |
| 83 | + label: category, |
| 84 | + children: [], |
| 85 | + sketches: [], |
| 86 | + }; |
| 87 | + await visit(path.join(destination, category), example); |
| 88 | + examples.push(example); |
| 89 | + } |
| 90 | + await fs.writeFile( |
| 91 | + path.join(destination, 'examples.json'), |
| 92 | + JSON.stringify(examples, null, 2), |
| 93 | + { encoding: 'utf8' } |
| 94 | + ); |
| 95 | + shell.echo(`Generated output to ${path.join(destination, 'examples.json')}`); |
33 | 96 | })();
|
0 commit comments