UNPKG

7.31 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.chmod = chmod;
7exports.readdir = readdir;
8exports.readdirForCompilable = readdirForCompilable;
9exports.isCompilableExtension = isCompilableExtension;
10exports.compile = compile;
11exports.deleteDir = deleteDir;
12exports.requireChokidar = requireChokidar;
13
14var _fsReaddirRecursive = _interopRequireDefault(require("fs-readdir-recursive"));
15
16var _chalk = _interopRequireDefault(require("chalk"));
17
18var _path4 = _interopRequireDefault(require("path"));
19
20var _fs = _interopRequireDefault(require("fs"));
21
22var _core = require("@elodin/core");
23
24var _error = require("./error");
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
27
28function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
29
30function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
31
32function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
33
34function _templateObject6() {
35 var data = _taggedTemplateLiteral(["", ""]);
36
37 _templateObject6 = function _templateObject6() {
38 return data;
39 };
40
41 return data;
42}
43
44function _templateObject5() {
45 var data = _taggedTemplateLiteral(["{red ", "}"]);
46
47 _templateObject5 = function _templateObject5() {
48 return data;
49 };
50
51 return data;
52}
53
54function _templateObject4() {
55 var data = _taggedTemplateLiteral(["The property ", " does not accept the value {bold ", "}.\n{dim Check {underline https://elodin.dev/docs/language/styles#", "} for a list of available values.}"]);
56
57 _templateObject4 = function _templateObject4() {
58 return data;
59 };
60
61 return data;
62}
63
64function _templateObject3() {
65 var data = _taggedTemplateLiteral(["", ": {red ", "}"]);
66
67 _templateObject3 = function _templateObject3() {
68 return data;
69 };
70
71 return data;
72}
73
74function _templateObject2() {
75 var data = _taggedTemplateLiteral(["The property {bold ", "} is not a valid ", " property.", "\n{dim Check {underline https://elodin.dev/docs/language/styles#", "} for a list of available properties.}"]);
76
77 _templateObject2 = function _templateObject2() {
78 return data;
79 };
80
81 return data;
82}
83
84function _templateObject() {
85 var data = _taggedTemplateLiteral(["{red ", "}: ", ""]);
86
87 _templateObject = function _templateObject() {
88 return data;
89 };
90
91 return data;
92}
93
94function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
95
96function chmod(src, dest) {
97 _fs["default"].chmodSync(dest, _fs["default"].statSync(src).mode);
98}
99
100function readdir(dirname, includeDotfiles, filter) {
101 return (0, _fsReaddirRecursive["default"])(dirname, function (filename, _index, currentDirectory) {
102 var stat = _fs["default"].statSync(_path4["default"].join(currentDirectory, filename));
103
104 if (stat.isDirectory()) return true;
105 return (includeDotfiles || filename[0] !== '.') && (!filter || filter(filename));
106 });
107}
108
109function readdirForCompilable(dirname, includeDotfiles) {
110 return readdir(dirname, includeDotfiles, isCompilableExtension);
111}
112
113function isCompilableExtension(filename) {
114 return _path4["default"].extname(filename) === '.elo';
115} // export function transform(filename, code, opts) {
116// opts = {
117// ...opts,
118// filename,
119// }
120// return new Promise((resolve, reject) => {
121// babel.transform(code, opts, (err, result) => {
122// if (err) reject(err)
123// else resolve(result)
124// })
125// })
126// }
127
128
129function compile(filename, opts) {
130 var log = function log(errors) {
131 return errors.forEach(function (error) {
132 var count = ++opts.errorCount;
133
134 if (error.type === _core.errorTypes.INVALID_PROPERTY) {
135 var property = error.property,
136 value = error.value,
137 _path = error.path,
138 line = error.line,
139 format = error.format,
140 hint = error.hint;
141 var didYouMeanMessage = hint ? "\n\n Did you mean: ".concat(hint, "?\n") : '';
142 (0, _error.logSyntaxError)({
143 count: count,
144 path: _path,
145 lineNumber: line,
146 line: (0, _chalk["default"])(_templateObject(), property, (0, _core.formatFromAST)(value)),
147 message: (0, _chalk["default"])(_templateObject2(), property, format, didYouMeanMessage, format)
148 });
149 } else if (error.type === _core.errorTypes.INVALID_VALUE) {
150 var _property = error.property,
151 _value = error.value,
152 _path2 = error.path,
153 _line = error.line,
154 _format = error.format;
155 (0, _error.logSyntaxError)({
156 count: count,
157 path: _path2,
158 lineNumber: _line,
159 line: (0, _chalk["default"])(_templateObject3(), _property, (0, _core.formatFromAST)(_value)),
160 message: (0, _chalk["default"])(_templateObject4(), _property, (0, _core.formatFromAST)(_value), _format)
161 });
162 } else {
163 var _line2 = error.line,
164 _path3 = error.path,
165 message = error.message,
166 source = error.source,
167 token = error.token;
168 var lineCode = source ? source.substr(0, token.end).split('\n').pop() : '';
169 (0, _error.logSyntaxError)({
170 count: count,
171 path: _path3,
172 lineNumber: _line2,
173 line: (0, _chalk["default"])(_templateObject5(), lineCode),
174 message: (0, _chalk["default"])(_templateObject6(), message)
175 });
176 }
177 });
178 };
179
180 return new Promise(function (resolve, reject) {
181 return (0, _core.transformFile)(filename, _objectSpread({}, opts, {
182 log: log
183 }), resolve);
184 });
185}
186
187function deleteDir(path) {
188 if (_fs["default"].existsSync(path)) {
189 _fs["default"].readdirSync(path).forEach(function (file) {
190 var curPath = path + '/' + file;
191
192 if (_fs["default"].lstatSync(curPath).isDirectory()) {
193 // recurse
194 deleteDir(curPath);
195 } else {
196 // delete file
197 _fs["default"].unlinkSync(curPath);
198 }
199 });
200
201 _fs["default"].rmdirSync(path);
202 }
203}
204
205process.on('uncaughtException', function (err) {
206 console.error(err);
207 process.exit(1);
208});
209
210function requireChokidar() {
211 try {
212 return require('chokidar');
213 } catch (err) {
214 console.error('The optional dependency chokidar failed to install and is required for ' + '--watch. Chokidar is likely not supported on your platform.');
215 throw err;
216 }
217}
\No newline at end of file