Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make single 'build.js' to handle NPM package build (#1841)
- Loading branch information
1 parent
1023264
commit c11ccba
Showing
6 changed files
with
145 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* 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. | ||
* | ||
* @noflow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const assert = require('assert'); | ||
const babel = require('@babel/core'); | ||
const { | ||
copyFile, | ||
writeFile, | ||
rmdirRecursive, | ||
mkdirRecursive, | ||
readdirRecursive, | ||
} = require('./fs-utils'); | ||
|
||
if (require.main === module) { | ||
rmdirRecursive('./dist'); | ||
mkdirRecursive('./dist'); | ||
|
||
copyFile('./LICENSE', './dist/LICENSE'); | ||
copyFile('./README.md', './dist/README.md'); | ||
writeFile('./dist/package.json', buildPackageJSON()); | ||
|
||
const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ }); | ||
for (const filepath of srcFiles) { | ||
if (filepath.endsWith('.js')) { | ||
buildJSFile(filepath); | ||
} | ||
} | ||
} | ||
|
||
function buildJSFile(filepath) { | ||
const srcPath = path.join('./src', filepath); | ||
const destPath = path.join('./dist', filepath); | ||
const cjs = babel.transformFileSync(srcPath, { envName: 'cjs' }); | ||
const mjs = babel.transformFileSync(srcPath, { envName: 'mjs' }); | ||
|
||
copyFile(srcPath, destPath + '.flow'); | ||
writeFile(destPath, cjs.code); | ||
writeFile(destPath.replace(/\.js$/, '.mjs'), mjs.code); | ||
} | ||
|
||
function buildPackageJSON() { | ||
const packageJSON = require('../package.json'); | ||
delete packageJSON.scripts; | ||
delete packageJSON.devDependencies; | ||
|
||
const { version } = packageJSON; | ||
const match = /^[0-9]+\.[0-9]+\.[0-9]+-?([^.]*)/.exec(version); | ||
assert(match, 'Version does not match semver spec.'); | ||
const tag = match[1]; | ||
assert(!tag || tag === 'rc', 'Only "rc" tag is supported.'); | ||
|
||
assert(!packageJSON.publishConfig, 'Can not override "publishConfig".'); | ||
packageJSON.publishConfig = { tag: tag || 'latest' }; | ||
return JSON.stringify(packageJSON, null, 2); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* 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. | ||
* | ||
* @noflow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function mkdirRecursive(path) { | ||
fs.mkdirSync(path, { recursive: true }); | ||
} | ||
|
||
function rmdirRecursive(dirPath) { | ||
if (fs.existsSync(dirPath)) { | ||
for (const dirent of fs.readdirSync(dirPath, { withFileTypes: true })) { | ||
const fullPath = path.join(dirPath, dirent.name); | ||
if (dirent.isDirectory()) { | ||
rmdirRecursive(fullPath); | ||
} else { | ||
fs.unlinkSync(fullPath); | ||
} | ||
} | ||
fs.rmdirSync(dirPath); | ||
} | ||
} | ||
|
||
function readdirRecursive(dirPath, opts = {}) { | ||
const { ignoreDir } = opts; | ||
return fs.readdirSync(dirPath, { withFileTypes: true }) | ||
.flatMap(dirent => { | ||
const name = dirent.name; | ||
if (dirent.isDirectory()) { | ||
if (ignoreDir && ignoreDir.test(name)) { | ||
return []; | ||
} | ||
return readdirRecursive(path.join(dirPath, name), opts) | ||
.map(f => path.join(name, f)); | ||
} | ||
return dirent.name; | ||
}); | ||
} | ||
|
||
function writeFile(destPath, data) { | ||
mkdirRecursive(path.dirname(destPath)); | ||
fs.writeFileSync(destPath, data); | ||
} | ||
|
||
function copyFile(srcPath, destPath) { | ||
mkdirRecursive(path.dirname(destPath)); | ||
fs.copyFileSync(srcPath, destPath); | ||
} | ||
|
||
module.exports = { | ||
copyFile, | ||
writeFile, | ||
rmdirRecursive, | ||
mkdirRecursive, | ||
readdirRecursive, | ||
}; |
Oops, something went wrong.