secure-scuttlebot classic
1var fs = require('fs')
2var path = require('path')
3
4function printRequireMap (opts) {
5 var options = opts || {}
6 var baseDir = options.baseDir
7 var targetDir = options.targetDir
8
9 if (!baseDir || typeof baseDir !== 'string')
10 throw new Error('printRequireMap requires opts.baseDir')
11 if (!targetDir || typeof targetDir !== 'string')
12 throw new Error('printRequireMap requires opts.targetDir')
13
14 var excludeName = options.excludeName
15 var jsOnly = !!options.jsOnly
16
17 var files = fs.readdirSync(path.join(baseDir, targetDir))
18 if (jsOnly) {
19 files = files.filter(function (file) {
20 if (excludeName && file === excludeName) return false
21 return /\.js$/.test(file)
22 })
23 }
24
25 console.log(
26 'module.exports = {\n'
27 +
28 files.map(function (file) {
29 return ' ' + JSON.stringify(file) + ": require('./" + file + "')"
30 }).join(',\n')
31 +
32 '\n}'
33 )
34}
35
36function writeStyleAssets (opts) {
37 var options = opts || {}
38 var appDir = options.appDir
39
40 if (!appDir || typeof appDir !== 'string')
41 throw new Error('writeStyleAssets requires opts.appDir')
42
43 var stylePath = path.join(appDir, 'style.css')
44 var styleJsonPath = path.join(appDir, 'style.css.json')
45 var style = fs.readFileSync(stylePath, 'utf8')
46
47 fs.writeFileSync(styleJsonPath, JSON.stringify(style))
48
49 if (options.copyCssToBuild) {
50 var buildDir = path.join(appDir, 'build')
51 if (!fs.existsSync(buildDir))
52 fs.mkdirSync(buildDir)
53 fs.writeFileSync(path.join(buildDir, 'style.css'), style)
54 }
55}
56
57module.exports = {
58 printRequireMap: printRequireMap,
59 writeStyleAssets: writeStyleAssets
60}