forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
117 lines (105 loc) · 3.5 KB
/
build.js
File metadata and controls
117 lines (105 loc) · 3.5 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
/**
* script to build (transpile) files.
*
* Based off of the build script from Metro, and tweaked to run in just one
* package instead of in a monorepo. Just run `build.js` and the JS files in
* `src/` will be built in `lib/`, and the original source files will be copied
* over as `Example.js.flow`, so consumers of this module can still make use of
* type checking.
*
* Call this script with the `--verbose` flag to show the full output of this
* script.
*/
'use strict';
const babel = require('@babel/core');
const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const micromatch = require('micromatch');
const mkdirp = require('mkdirp');
const path = require('path');
const prettier = require('prettier');
const prettierConfig = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '..', '.prettierrc'), 'utf8'),
);
const SRC_DIR = 'src';
const BUILD_DIR = 'lib';
const JS_FILES_PATTERN = '**/*.js';
const IGNORE_PATTERN = '**/__tests__/**';
const PACKAGE_DIR = path.resolve(__dirname, '../');
const fixedWidth = str => {
const WIDTH = 80;
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g')) || [str];
let lastString = strs[strs.length - 1];
if (lastString.length < WIDTH) {
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
}
return strs
.slice(0, -1)
.concat(lastString)
.join('\n');
};
function getBuildPath(file, buildFolder) {
const pkgSrcPath = path.resolve(PACKAGE_DIR, SRC_DIR);
const pkgBuildPath = path.resolve(PACKAGE_DIR, BUILD_DIR);
const relativeToSrcPath = path.relative(pkgSrcPath, file);
return path.resolve(pkgBuildPath, relativeToSrcPath);
}
function buildFile(file, silent) {
const destPath = getBuildPath(file, BUILD_DIR);
mkdirp.sync(path.dirname(destPath));
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
silent ||
process.stdout.write(
chalk.dim(' \u2022 ') +
path.relative(PACKAGE_DIR, file) +
' (ignore)\n',
);
} else if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
silent ||
process.stdout.write(
chalk.red(' \u2022 ') +
path.relative(PACKAGE_DIR, file) +
chalk.red(' \u21D2 ') +
path.relative(PACKAGE_DIR, destPath) +
' (copy)' +
'\n',
);
} else {
const transformed = prettier.format(
babel.transformFileSync(file, {}).code,
{
...prettierConfig,
parser: 'babel',
},
);
fs.writeFileSync(destPath, transformed);
const source = fs.readFileSync(file).toString('utf-8');
if (/\@flow/.test(source)) {
fs.createReadStream(file).pipe(fs.createWriteStream(destPath + '.flow'));
}
silent ||
process.stdout.write(
chalk.green(' \u2022 ') +
path.relative(PACKAGE_DIR, file) +
chalk.green(' \u21D2 ') +
path.relative(PACKAGE_DIR, destPath) +
'\n',
);
}
}
const srcDir = path.resolve(__dirname, '..', SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {nodir: true});
process.stdout.write(fixedWidth(`${path.basename(PACKAGE_DIR)}\n`));
files.forEach(file => buildFile(file, !process.argv.includes('--verbose')));
process.stdout.write(`[ ${chalk.green('OK')} ]\n`);