Debian Codegraph @

components/bin/copy

raw
1 #! /usr/bin/env node
2
3 /*************************************************************
4 *
5 * Copyright (c) 2018 The MathJax Consortium
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 /**
21 * @fileoverview Copies specified files to the distribution directory
22 *
23 * @author [email protected] (Davide Cervone)
24 */
25
26
27 const fs = require('fs');
28 const path = require('path');
29
30 /**
31 * The amount of space for each level of indentation
32 */
33 const INDENT = ' ';
34
35 /**
36 * The configuration data for the copy operation
37 */
38 const config = JSON.parse(fs.readFileSync(process.argv[2] || 'copy.json'));
39
40 /**
41 * Get the directory for node modules (either the parent of the MathJax directory,
42 * or the MathJax node_modules directory, if it exists).
43 */
44 const parent = path.resolve(__dirname, '..', '..');
45 const nodeDir = (dir => (fs.existsSync(dir) ? dir : path.resolve(parent, '..')))(path.join(parent, 'node_modules'));
46
47 /**
48 * Copy a file or directory tree
49 *
50 * @param {string} from The directory to copy from
51 * @param {string} to The directory to copy to
52 * @param {string} name The name of the file or directory to copy
53 * @param {string} space The indentation for output
54 */
55 function copyFile(from, to, name, space) {
56 !fs.existsSync(to) && fs.mkdirSync(to, {recursive: true});
57 const copy = path.resolve(from, name);
58 const dest = path.resolve(to, name);
59 if (fs.lstatSync(copy).isDirectory()) {
60 console.info(space + name + '/');
61 for (const file of fs.readdirSync(copy)) {
62 copyFile(copy, dest, file, space + INDENT);
63 }
64 } else {
65 console.info(space + name);
66 fs.copyFileSync(copy, dest);
67 }
68 }
69
70 /**
71 * Copy the given files
72 */
73 const wd = process.cwd();
74 const to = path.resolve(wd, config.to);
75 const from = path.resolve(wd, config.from.replace(/\[node\]/, nodeDir));
76 for (const name of config.copy) {
77 copyFile(from, to, name, '');
78 }