1 | 'use strict'
|
2 |
|
3 | const fs = require('fs-extra')
|
4 | const path = require('path')
|
5 | const glob = require('glob')
|
6 | const { execa } = require('@mara/devkit')
|
7 | const C = require('../config/const')
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const appDirectory = fs.realpathSync(process.cwd())
|
15 |
|
16 | function rootPath(relativePath) {
|
17 | return path.resolve(appDirectory, relativePath)
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function getViews(entryGlob) {
|
25 | const entries = getEntries(`${process.cwd()}/${entryGlob}`)
|
26 | return Object.keys(entries)
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function getEntries(globPath, preDep = []) {
|
40 | const files = glob.sync(rootPath(globPath))
|
41 | const hasPreDep = preDep.length > 0
|
42 | const getViewName = filepath => {
|
43 | const dirname = path.dirname(path.relative(`${C.VIEWS_DIR}/`, filepath))
|
44 |
|
45 | return dirname === '..' ? 'index' : dirname
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | return files.reverse().reduce((entries, filepath) => {
|
52 | const name = getViewName(filepath)
|
53 |
|
54 | entries[name] = hasPreDep ? [].concat(preDep, filepath) : filepath
|
55 |
|
56 | return entries
|
57 | }, {})
|
58 | }
|
59 |
|
60 | function getEntryPoints(globPath, preDep = []) {
|
61 | const files = glob.sync(rootPath(globPath))
|
62 | const getChunkName = filepath => {
|
63 | const extname = path.extname(filepath)
|
64 | const basename = path.posix.basename(filepath, extname)
|
65 |
|
66 | return basename.replace(/^index\./, '') + '.servant'
|
67 | }
|
68 |
|
69 | return files.reduce((chunks, filepath) => {
|
70 | const name = getChunkName(filepath)
|
71 |
|
72 | chunks[name] = [].concat(preDep, filepath)
|
73 |
|
74 | return chunks
|
75 | }, {})
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function parseDate(target) {
|
84 | const f = n => (n > 9 ? n : '0' + n)
|
85 | const date = target instanceof Date ? target : new Date(target)
|
86 | return {
|
87 | y: date.getFullYear(),
|
88 | M: f(date.getMonth() + 1),
|
89 | d: f(date.getDate()),
|
90 | h: f(date.getHours()),
|
91 | m: f(date.getMinutes()),
|
92 | s: f(date.getSeconds())
|
93 | }
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function pubDate(dt) {
|
102 | const date = parseDate(dt)
|
103 | return `${date.y}-${date.M}-${date.d}`
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function banner(version, target = '') {
|
111 | return (
|
112 | `@version ${version}\n` +
|
113 | `@date ${pubDate(new Date())}\n` +
|
114 |
|
115 |
|
116 | `@generated ${target}`
|
117 | )
|
118 | }
|
119 |
|
120 | function isNotEmptyArray(target) {
|
121 | return Array.isArray(target) && target.length
|
122 | }
|
123 |
|
124 | function isObject(obj) {
|
125 | return Object.prototype.toString.call(obj) === '[object Object]'
|
126 | }
|
127 |
|
128 | function assetsPath(_path) {
|
129 | return path.posix.join('static', _path)
|
130 | }
|
131 |
|
132 | function readJson(file) {
|
133 | return fs.readJsonSync(file, { throws: false })
|
134 | }
|
135 |
|
136 | function sortObject(obj, keyOrder, dontSortByUnicode) {
|
137 | if (!obj) return
|
138 | const res = {}
|
139 |
|
140 | if (keyOrder) {
|
141 | keyOrder.forEach(key => {
|
142 | res[key] = obj[key]
|
143 | delete obj[key]
|
144 | })
|
145 | }
|
146 |
|
147 | const keys = Object.keys(obj)
|
148 |
|
149 | !dontSortByUnicode && keys.sort()
|
150 | keys.forEach(key => {
|
151 | res[key] = obj[key]
|
152 | })
|
153 |
|
154 | return res
|
155 | }
|
156 |
|
157 | function relativePath(filePath) {
|
158 | return '.' + path.sep + path.relative(appDirectory, filePath)
|
159 | }
|
160 |
|
161 | function bumpProjectVersion(version = 'prerelease') {
|
162 | return execa.sync('npm', ['--no-git-tag-version', 'version', version])
|
163 | }
|
164 |
|
165 | function isInstalled(name) {
|
166 | try {
|
167 | require.resolve(name)
|
168 | return true
|
169 | } catch (e) {
|
170 | return false
|
171 | }
|
172 | }
|
173 |
|
174 | module.exports = {
|
175 | assetsPath,
|
176 | bumpProjectVersion,
|
177 | isObject,
|
178 | getViews,
|
179 | getEntries,
|
180 | getEntryPoints,
|
181 | rootPath,
|
182 | parseDate,
|
183 | pubDate,
|
184 | banner,
|
185 | isNotEmptyArray,
|
186 | isInstalled,
|
187 | sortObject,
|
188 | readJson,
|
189 | relativePath
|
190 | }
|