1 | 1 | 'use strict'; |
2 | | |
3 | 1 | var path = require('path'); |
4 | | |
5 | 1 | var babel = require('babel-core'); |
6 | 1 | var browserify = require('browserify'); |
7 | | |
8 | 1 | var babelify = require('babelify'); |
9 | 1 | var es2015 = require('babel-preset-es2015'); |
10 | 1 | var react = require('babel-preset-react'); |
11 | | |
12 | 1 | var TRANSFORM_DEFAULTS = { |
13 | | bundle: false |
14 | | }; |
15 | | |
16 | | /** |
17 | | * Transforms a file with babel. |
18 | | * |
19 | | * @example spire.transformFileWithBabel(file).then(function (result) { console.log(result); }); |
20 | | * @param {String} file File path. |
21 | | * @return {Object} Promise |
22 | | * @public |
23 | | */ |
24 | | |
25 | 1 | var transformFileWithBabel = function transformFileWithBabel(file) { |
26 | | |
27 | 10 | return new Promise(function (resolve, reject) { |
28 | | |
29 | 10 | babel.transformFile(file, { |
30 | | presets: [es2015, react] |
31 | | }, function (err, result) { |
32 | | |
33 | 10 | if (err) { |
34 | 1 | return reject(new Error(err)); |
35 | | } |
36 | | |
37 | 9 | resolve(result.code); |
38 | | }); |
39 | | }); |
40 | | }; |
41 | | |
42 | | /** |
43 | | * Transforms a file with browserify. |
44 | | * |
45 | | * @example spire.transformFileWithBrowserify(file).then(function (result) { console.log(result); }); |
46 | | * @param {String} file File path. |
47 | | * @return {Object} Promise |
48 | | * @public |
49 | | */ |
50 | | |
51 | 1 | var transformFileWithBrowserify = function transformFileWithBrowserify(file) { |
52 | | |
53 | 2 | return new Promise(function (resolve, reject) { |
54 | | |
55 | | browserify(file).transform(babelify, { |
56 | | presets: [es2015, react] |
57 | 2 | }).bundle(function (err, buffer) { |
58 | | |
59 | 2 | if (err) { |
60 | 1 | return reject(new Error(err)); |
61 | | } |
62 | | |
63 | 1 | resolve(buffer.toString()); |
64 | | }); |
65 | | }); |
66 | | }; |
67 | | |
68 | | /** |
69 | | * Transforms a file. |
70 | | * |
71 | | * @example spire.transformFile(file, options).then(function (result) { console.log(result); }); |
72 | | * @param {String} file File path. |
73 | | * @param {Object} [options] Options object. |
74 | | * @param {Boolean} [options.bundle] Use browserify bundler. |
75 | | * @return {Object} Promise |
76 | | * @public |
77 | | */ |
78 | | |
79 | 1 | var transformFile = function transformFile(file, options) { |
80 | | |
81 | 12 | var settings = Object.assign({}, TRANSFORM_DEFAULTS, options); |
82 | | |
83 | 12 | if (settings.bundle) { |
84 | | |
85 | 2 | return transformFileWithBrowserify(file, options); |
86 | | } else { |
87 | | |
88 | 10 | return transformFileWithBabel(file, options); |
89 | | } |
90 | | }; |
91 | | |
92 | | /** |
93 | | * Parses a path into directory and filename or file regular expression pattern. |
94 | | * |
95 | | * @example console.log(spire.parseWatchPath(input)); |
96 | | * @param {String} input Path to parse. |
97 | | * @return {Object} Object with directory, filename (pattern) and boolean flag. |
98 | | * @public |
99 | | */ |
100 | | |
101 | 1 | var parseWatchPath = function parseWatchPath(input) { |
102 | | |
103 | 3 | var directory = null; |
104 | 3 | var filename = null; |
105 | | |
106 | 3 | var recursive = false; |
107 | 3 | var recursivePattern = /\/\*\*\/\/*/; |
108 | | |
109 | 3 | if (input.match(recursivePattern)) { |
110 | | |
111 | 1 | recursive = true; |
112 | | } |
113 | | |
114 | 3 | directory = path.dirname(input.replace(recursivePattern, '/')); |
115 | 3 | filename = path.basename(input).replace(/^\*/, ''); |
116 | | |
117 | 3 | return { |
118 | | directory: directory, |
119 | | filename: filename, |
120 | | recursive: recursive |
121 | | }; |
122 | | }; |
123 | | |
124 | 1 | module.exports = { |
125 | | transformFileWithBabel: transformFileWithBabel, |
126 | | transformFileWithBrowserify: transformFileWithBrowserify, |
127 | | transformFile: transformFile, |
128 | | parseWatchPath: parseWatchPath |
129 | | }; |