UNPKG

2.08 kBJavaScriptView Raw
1'use strict';
2/**
3 * Js文件处理
4 *
5 * @class Pug2html
6 * {
7 * src:'', <string> 源文件路径
8 * dist:'', <string> 输出路径
9 * }
10 */
11class Js{
12 constructor(option){
13 const _ts = this;
14
15 option = option || {};
16
17 let m = _ts.m = {
18 fs:require('fs-extra'),
19 path:require('path'),
20 replaceGlobal:require('./replaceGlobal')
21 },
22 config = _ts.config = {};
23
24 //配置写入到_ts.config
25 for(let i in option){
26 config[i] = option[i];
27 };
28 let src = config.src,
29 dist = config.dist,
30 distDir = m.path.dirname(dist),
31 jsContent = m.fs.readFileSync(src).toString();
32
33 return new Promise((resolve,reject)=>{
34 if(jsContent){
35 jsContent = m.replaceGlobal(jsContent);
36 m.fs.ensureDir(distDir,(err)=>{
37 if(err){
38 reject({
39 status:'error',
40 msg:`创建失败 ${distDir}`,
41 info:err
42 });
43 };
44
45 try {
46 m.fs.writeFileSync(dist,jsContent);
47 resolve({
48 status:'success',
49 msg:`写入 ${dist}`,
50 data:jsContent,
51 distPath:dist
52 });
53 } catch (err) {
54 reject({
55 status:'error',
56 msg:`写入失败 ${dist}`,
57 info:err
58 });
59 };
60 });
61 }else{
62 resolve({
63 status:'success',
64 msg:`为空 ${src} 无需写入`,
65 data:jsContent
66 });
67 };
68 });
69 }
70}
71module.exports = Js;