UNPKG

1.32 kBJavaScriptView Raw
1'use strict';
2const fs = require('fs');
3const path = require('path');
4
5const esprima = require('espree');
6const escodegen = require('escodegen');
7
8const esprimaOptions = {
9 sourceType: 'module',
10 ecmaVersion: 6
11}
12
13const escodegenOptions = {
14 comment: true,
15 format: {
16 indent: { style: ' ' },
17 semicolons: true,
18 }
19}
20
21const read = path => esprima.parse(fs.readFileSync(path, 'utf8'), esprimaOptions)
22const readString = str => esprima.parse(str, esprimaOptions)
23
24const write = function(path, tree) {
25 const code = escodegen.generate(tree, escodegenOptions) + '\n';
26
27 fs.writeFileSync(path, code, 'utf8');
28};
29
30const 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
36const 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
42const getBaseName = function(path) {
43 const items = path.split('/');
44 return items[items.length - 1];
45};
46
47module.exports = {
48 read,
49 readString,
50 write,
51 getDestinationPath,
52 getBaseName,
53 getRelativePath
54}