1 | let Path = require('path')
|
2 | let fs = require('fs-extra')
|
3 |
|
4 |
|
5 | module.exports = {
|
6 | log: require('./log'),
|
7 | getVer() {
|
8 | let pkg = Path.join(__dirname, '../package.json')
|
9 | return require(pkg).version
|
10 | },
|
11 | getProjectConfig() {
|
12 | let config = Path.resolve('./jane.config.js')
|
13 | if (!fs.existsSync(config)) {
|
14 | return false
|
15 | }
|
16 | return require(config)
|
17 | },
|
18 | getProjectPkg() {
|
19 | let config = Path.resolve('./package.json')
|
20 | if (!fs.existsSync(config)) {
|
21 | return false
|
22 | }
|
23 | return require(config)
|
24 | },
|
25 | getDistPath() {
|
26 | return this.getProjectConfig().dest || 'build'
|
27 | },
|
28 | getSrcPath() {
|
29 | return this.getProjectConfig().src || 'src'
|
30 | },
|
31 | getModulesPath() {
|
32 | return 'node_modules'
|
33 | },
|
34 | write() {
|
35 |
|
36 | return fs.outputFile.apply(this, arguments)
|
37 | },
|
38 | getOutputFile(str) {
|
39 | let dest = this.getDistPath()
|
40 | let src = this.getSrcPath()
|
41 | let modules = this.getModulesPath()
|
42 | if (str.indexOf(modules) > -1) {
|
43 | str = `${dest}/${str}`
|
44 | }
|
45 | str = str.replace(modules, 'npm')
|
46 | return str.replace(src, dest)
|
47 | },
|
48 | isDir(path){
|
49 | let r = false
|
50 | try{
|
51 | r = fs.lstatSync(path)&&fs.lstatSync(path).isDirectory()
|
52 | }
|
53 | catch(err){
|
54 |
|
55 | }
|
56 | return r
|
57 | },
|
58 | isFile(path) {
|
59 | let r = false
|
60 | try{
|
61 | r = fs.lstatSync(path)&&fs.lstatSync(path).isFile()
|
62 | }
|
63 | catch(err){
|
64 |
|
65 | }
|
66 | return r
|
67 | },
|
68 | jsRequire(path){
|
69 | if(this.isFile(path)){
|
70 | return path
|
71 | }
|
72 | else if(this.isFile(`${path}.js`)){
|
73 | return `${path}.js`
|
74 | }
|
75 | else if(this.isFile(`${path}/index.js`)){
|
76 | return `${path}/index.js`
|
77 | }
|
78 | return false
|
79 | }
|
80 | }
|