1 | 'use strict';
|
2 | const fs = require('fs');
|
3 | const path = require('path');
|
4 |
|
5 | const esprima = require('espree');
|
6 | const escodegen = require('escodegen');
|
7 |
|
8 | const esprimaOptions = {
|
9 | sourceType: 'module',
|
10 | ecmaVersion: 6
|
11 | }
|
12 |
|
13 | const escodegenOptions = {
|
14 | comment: true,
|
15 | format: {
|
16 | indent: { style: ' ' },
|
17 | semicolons: true,
|
18 | }
|
19 | }
|
20 |
|
21 | const read = path => esprima.parse(fs.readFileSync(path, 'utf8'), esprimaOptions)
|
22 | const readString = str => esprima.parse(str, esprimaOptions)
|
23 |
|
24 | const write = function(path, tree) {
|
25 | const code = escodegen.generate(tree, escodegenOptions) + '\n';
|
26 |
|
27 | fs.writeFileSync(path, code, 'utf8');
|
28 | };
|
29 |
|
30 | const getDestinationPath = function(name, type, suffix) {
|
31 | const prefix = path.join('src', type, name);
|
32 | const portablePrefix = path.sep === '/' ? prefix : prefix.split(path.sep).join('/');
|
33 | return [portablePrefix, suffix].join('.');
|
34 | };
|
35 |
|
36 | const getRelativePath = function(name, type, suffix) {
|
37 | const filePath = path.join('..', type, name);
|
38 | const portableFilePath = path.sep === '/' ? filePath : filePath.split(path.sep).join('/');
|
39 | return [portableFilePath, suffix].join('.');
|
40 | };
|
41 |
|
42 | const getBaseName = function(path) {
|
43 | const items = path.split('/');
|
44 | return items[items.length - 1];
|
45 | };
|
46 |
|
47 | module.exports = {
|
48 | read,
|
49 | readString,
|
50 | write,
|
51 | getDestinationPath,
|
52 | getBaseName,
|
53 | getRelativePath
|
54 | }
|