1 |
|
2 |
|
3 | const
|
4 | TEMP_FILE_NAME = require('./const').TEMP_FILE_NAME, // skip temporary files (created by editors), e.g. /.name.tag, /~name.tag, /name~.tag
|
5 | path = require('path'),
|
6 | rollup = require('rollup'),
|
7 | chalk = require('chalk'),
|
8 | sh = require('shelljs')
|
9 |
|
10 | module.exports = {
|
11 |
|
12 | /**
|
13 | * Find any file in certain folder
|
14 | * @param { RegExp } extRegex - regular expression containing the file extension
|
15 | * @param { String } from - files path
|
16 | * @returns { Array } array containing the files found
|
17 | */
|
18 | find(extRegex, from) {
|
19 | return sh
|
20 | .find(from)
|
21 | .filter((f) => extRegex.test(f) && TEMP_FILE_NAME.test(f))
|
22 | },
|
23 |
|
24 | /**
|
25 | * Loop files paths strings contained in an array remapping them to a different location
|
26 | * @param { RegExp } extRegex - regular expression containing the file extension
|
27 | * @param { String } from - path where the files are located
|
28 | * @param { String } to - path where the new files must be created
|
29 | * @param { String } base - base path
|
30 | * @param { String } extension - base path
|
31 | * @returns { Array } array containing all the paths to the new files that must be created
|
32 | */
|
33 | remap(extRegex, from, to, base, extension) {
|
34 | return from
|
35 | .map((from) => path.join(to, path.relative(base, from)
|
36 | .replace(extRegex, `.${extension || 'js'}`)))
|
37 | },
|
38 |
|
39 | /**
|
40 | * Relative path to where the command line gets executed
|
41 | * @param { String } path - the whole file path where a file is located on the machine
|
42 | * @returns { String } path relative to the current folder where the command line gets executed
|
43 | */
|
44 | toRelative(path) {
|
45 | return path.replace(sh.pwd().toString() + '/', '')
|
46 | },
|
47 |
|
48 | /**
|
49 | * Extend any object with other properties
|
50 | * @param { Object } src - source object
|
51 | * @returns { Object } the resulting extended object
|
52 | *
|
53 | * var obj = { foo: 'baz' }
|
54 | * extend(obj, {bar: 'bar', foo: 'bar'})
|
55 | * console.log(obj) => {bar: 'bar', foo: 'bar'}
|
56 | *
|
57 | */
|
58 | extend(src) {
|
59 | var obj, args = arguments
|
60 | for (var i = 1; i < args.length; ++i) {
|
61 | if (obj = args[i]) {
|
62 | for (var key in obj) {
|
63 | if (typeof obj[key] === 'object' && typeof src[key] === 'object')
|
64 | src[key] = this.extend(src[key], obj[key])
|
65 | else if (typeof obj[key] !== 'undefined')
|
66 | src[key] = obj[key]
|
67 | }
|
68 | }
|
69 | }
|
70 | return src
|
71 | },
|
72 |
|
73 | /**
|
74 | * Try to read the option from a file
|
75 | * @param { String } src - path to the config file
|
76 | * @returns { Object } cli options options
|
77 | */
|
78 | loadConfigFile(src) {
|
79 | src = path.resolve(src)
|
80 |
|
81 | // add the extension if it's missing
|
82 | if (src.slice(-3) !== '.js') src += '.js'
|
83 |
|
84 | // borrowed from the rollup cli
|
85 | // https://github.com/rollup/rollup/blob/master/bin/runRollup.js
|
86 | return rollup.rollup({
|
87 | entry: src,
|
88 | onwarn: this.log
|
89 | }).then((bundle) => {
|
90 | var
|
91 | opts,
|
92 | code = bundle.generate({
|
93 | format: 'cjs'
|
94 | }).code,
|
95 | // temporarily override require
|
96 | jsLoader = require.extensions['.js']
|
97 |
|
98 |
|
99 | require.extensions['.js' ]= function(m, filename) {
|
100 | if (filename === src) m._compile(code, filename)
|
101 | else jsLoader(m, filename)
|
102 | }
|
103 |
|
104 | try {
|
105 | opts = require(src)
|
106 | } catch (err) {
|
107 | this.err(err)
|
108 | }
|
109 |
|
110 | require.extensions['.js'] = jsLoader
|
111 |
|
112 | return opts
|
113 |
|
114 | }).catch((err) => {
|
115 | this.log('It was not possible to load your config file, are you sure the path is correct?')
|
116 | this.err(err)
|
117 | })
|
118 |
|
119 | },
|
120 | /**
|
121 | * Helper to output stuff in the terminal
|
122 | * @param { * } msg - normally this should be a string
|
123 | */
|
124 | log(msg) {
|
125 | /* istanbul ignore next */
|
126 | if (!global.isSilent) console.log(msg)
|
127 | },
|
128 | /**
|
129 | * Throw an error and kill the process
|
130 | * @param { String } msg - error message
|
131 | */
|
132 | err(msg) {
|
133 | msg += '\n'
|
134 | /* istanbul ignore next */
|
135 | if (!global.isSilent) process.stderr.write(chalk.red(msg)) || process.exit(1)
|
136 | else throw msg
|
137 | },
|
138 | /**
|
139 | * Get the current riot-cli release
|
140 | * @returns { String } this should always return the riot version in use unless riot-cli gets used as standalone module
|
141 | */
|
142 | getVersion() {
|
143 | return `
|
144 | riot-cli: ${require('../package.json').version} - https://github.com/riot/cli
|
145 | riot-compiler: ${require('riot-compiler/package.json').version} - https://github.com/riot/compiler
|
146 | `
|
147 | }
|
148 | }
|