1 | var la = require('lazy-ass')
|
2 | var is = require('check-more-types')
|
3 | var path = require('path')
|
4 | var join = path.join
|
5 | var fs = require('fs')
|
6 |
|
7 | var SELF_NAME = 'compiled'
|
8 |
|
9 | function bundleName (filename) {
|
10 | la(is.unemptyString(filename), 'expected filename', filename)
|
11 | return path.basename(filename, '.js')
|
12 | }
|
13 |
|
14 | function builtName (name) {
|
15 | la(is.unemptyString(name), 'expected name', name)
|
16 | return name + '.bundle.js'
|
17 | }
|
18 |
|
19 | function featuresName (name) {
|
20 | la(is.unemptyString(name), 'expected name', name)
|
21 | return name + '.features.json'
|
22 | }
|
23 |
|
24 | function compiledName (name) {
|
25 | la(is.unemptyString(name), 'expected name', name)
|
26 | return name + '.compiled.js'
|
27 | }
|
28 |
|
29 | function getFirstLine (filename) {
|
30 | var text = fs.readFileSync(filename, 'utf-8')
|
31 | return text.substr(0, text.indexOf('\n'))
|
32 | }
|
33 |
|
34 | function isHashbang (line) {
|
35 | return /^#!/.test(line)
|
36 | }
|
37 |
|
38 | function removeFirstLine (filename) {
|
39 | var text = fs.readFileSync(filename, 'utf-8')
|
40 | text = text.substr(text.indexOf('\n') + 1)
|
41 | fs.writeFileSync(filename, text, 'utf-8')
|
42 | }
|
43 |
|
44 | function restoreFirstLine (filename, line) {
|
45 | la(is.unemptyString(line), 'missing first line to add to', filename)
|
46 | var text = fs.readFileSync(filename, 'utf-8')
|
47 | text = line + '\n' + text
|
48 | fs.writeFileSync(filename, text, 'utf-8')
|
49 | }
|
50 |
|
51 |
|
52 | function endsWithNewLines (text) {
|
53 | return /\n\n$/.test(text)
|
54 | }
|
55 |
|
56 | function finishTextWithEndline (text) {
|
57 | la(is.string(text), 'expected text', text)
|
58 | if (!endsWithNewLines(text)) {
|
59 | text += '\n'
|
60 | }
|
61 | return text
|
62 | }
|
63 |
|
64 | function finishWithEndline (filename) {
|
65 | var text = fs.readFileSync(filename, 'utf-8')
|
66 | text = finishTextWithEndline(text)
|
67 | fs.writeFileSync(filename, text, 'utf-8')
|
68 | }
|
69 |
|
70 | function formFilenames (config, filename) {
|
71 | la(is.object(config), 'missing config object', config)
|
72 | la(is.unemptyString(filename), 'expected filename', filename)
|
73 |
|
74 | var joinDir = join.bind(null, config.dir)
|
75 |
|
76 | var name = bundleName(filename)
|
77 | var builtFilename = joinDir(builtName(name))
|
78 | var featuresFilename = joinDir(featuresName(name))
|
79 |
|
80 | var compiledFilename = is.fn(config.formOutputFilename)
|
81 | ? config.formOutputFilename(name)
|
82 | : joinDir(compiledName(name))
|
83 |
|
84 | return {
|
85 | built: builtFilename,
|
86 | features: featuresFilename,
|
87 | compiled: compiledFilename
|
88 | }
|
89 | }
|
90 |
|
91 | function addBabelRequire (text, moduleName) {
|
92 | text = text.trim()
|
93 |
|
94 | moduleName = moduleName || SELF_NAME
|
95 | var name = isSelfCompiling() ? '../src/compiled' : moduleName
|
96 |
|
97 | var requireLine = 'require(\'' + name + '\').babelPolyfill()\n'
|
98 | var firstNewLine = text.indexOf('\n')
|
99 | var firstLine = text.substr(0, firstNewLine + 1)
|
100 |
|
101 | var result
|
102 |
|
103 | if (/use strict/.test(firstLine)) {
|
104 | return firstLine + requireLine + text.substr(firstNewLine + 1)
|
105 | }
|
106 | if (isHashbang(firstLine)) {
|
107 | result = firstLine + '\n' + addBabelRequire(text.substr(firstNewLine + 1), moduleName)
|
108 | return result
|
109 | }
|
110 | return requireLine + text
|
111 | }
|
112 |
|
113 | function isSelfCompiling () {
|
114 | var packageFilename = join(process.cwd(), 'package.json')
|
115 | if (fs.existsSync(packageFilename)) {
|
116 | var pkg = JSON.parse(fs.readFileSync(packageFilename))
|
117 | return pkg.name === SELF_NAME
|
118 | }
|
119 | }
|
120 |
|
121 | module.exports = {
|
122 | bundleName: bundleName,
|
123 | builtName: builtName,
|
124 | featuresName: featuresName,
|
125 | compiledName: compiledName,
|
126 | getFirstLine: getFirstLine,
|
127 | isHashbang: isHashbang,
|
128 | removeFirstLine: removeFirstLine,
|
129 | restoreFirstLine: restoreFirstLine,
|
130 | finishWithEndline: finishWithEndline,
|
131 | finishTextWithEndline: finishTextWithEndline,
|
132 | formFilenames: formFilenames,
|
133 | addBabelRequire: addBabelRequire,
|
134 | isSelfCompiling: isSelfCompiling
|
135 | }
|