-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·133 lines (114 loc) · 3.6 KB
/
index.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
const { spawnSync } = require('child_process')
const { relative, resolve } = require('path')
const { readFileSync } = require('fs')
const { copySync, outputFileSync } = require('fs-extra')
const babel = require('@babel/core')
const chalk = require('chalk')
const minimatch = require('minimatch')
const parseArgs = require('minimist')
const { walkSync } = require('@hon2a/walk-sync')
const escapeRegExp = require('lodash.escaperegexp')
const { MODULE, LIB } = require('./env')
//region Config
const { cyan, green, red, white } = chalk
const path = cyan
const {
_: [sourceFolder = 'src'],
verbose,
extensions
} = parseArgs(process.argv.slice(2), {
boolean: ['verbose'],
string: ['extensions'],
default: { extensions: '.js' }
})
const moduleFolder = 'es'
const libFolder = 'lib'
const cwd = resolve('./')
const src = resolve(sourceFolder)
const mod = resolve(moduleFolder)
const lib = resolve(libFolder)
const partialConfig = babel.loadPartialConfig({
filename: resolve('./package.json')
})
const useDefaultBabelConfig = !partialConfig.hasFilesystemConfig()
const configFile = useDefaultBabelConfig
? resolve(__dirname, 'babel.config.js')
: undefined
//endregion
//region Helpers
const log = (...args) => console.log(white.dim('transpile-js:'), ...args) // eslint-disable-line no-console
const extensionRegExp = new RegExp(
`(?:${extensions.split(',').map(escapeRegExp).join('|')})$`
)
function transpileJs(filename, relativePath) {
const outputPath = relativePath.replace(extensionRegExp, '.js')
const code = readFileSync(filename, 'utf8')
const { ast } = babel.transformSync(code, {
filename,
// `modules: false` apparently needs to be set in both the AST parsing and the code transform steps.
// If it's missing in either one, ES6 module statements (import/export) get transformed.
envName: MODULE,
ast: true,
code: false,
configFile
})
const { code: moduleCode } = babel.transformFromAstSync(ast, code, {
filename,
envName: MODULE,
sourceMaps: false,
configFile
})
outputFileSync(resolve(mod, outputPath), moduleCode)
const { code: libCode } = babel.transformFromAstSync(ast, code, {
filename,
envName: LIB,
sourceMaps: false,
configFile
})
outputFileSync(resolve(lib, outputPath), libCode)
}
function copyAsset(filename, relativePath) {
copySync(filename, resolve(mod, relativePath))
copySync(filename, resolve(lib, relativePath))
}
//endregion
//region Script
log(
`Cleaning files from previous build (${path(moduleFolder)}, ${path(
libFolder
)}).`
)
spawnSync('rm', ['-rf', moduleFolder, libFolder])
log(
useDefaultBabelConfig
? `Babel config not found -> using default config.`
: `Babel config found in ${path(
relative(cwd, partialConfig.babelrc || partialConfig.config)
)}.`
)
const ignore = [`**/*.test${extensions}`, `**/*.test${extensions}.snap`]
const rules = [
[`**/*${extensions}`, transpileJs, 'transpiled'],
['**/*', copyAsset, 'copied']
]
let processedCount = 0
try {
for (const absolutePath of walkSync(src)) {
const [, task, msg = 'ignored'] =
(!ignore.find((glob) => minimatch(absolutePath, glob)) &&
rules.find((rule) => minimatch(absolutePath, rule[0]))) ||
[]
const relativePath = relative(src, absolutePath)
if (task) {
task(absolutePath, relativePath)
++processedCount
}
if (verbose) log(`${path(relativePath)} ... ${msg}`)
}
} catch (e) {
log(`${red('✖ Failed.')}\n${e.message ? `${e.name}: ${e.message}` : e}`)
process.exit(1)
}
log(`${green('√ Done.')} ${processedCount} sources are ready for publishing.`)
//endregion