1 | const fs = require('fs')
|
2 | const path = require('path')
|
3 | const chalk = require('chalk')
|
4 | const tildify = require('tildify')
|
5 | const AppError = require('./app-error')
|
6 |
|
7 | exports.cwd = function (cwd, ...args) {
|
8 | return path.resolve(cwd || process.cwd(), ...args)
|
9 | }
|
10 |
|
11 | exports.ownDir = function (...args) {
|
12 | return path.join(__dirname, '../', ...args)
|
13 | }
|
14 |
|
15 | exports.getConfigFile = function (config) {
|
16 | return exports.cwd(typeof config === 'string' ? config : 'poi.config.js')
|
17 | }
|
18 |
|
19 | exports.getPublicPath = function (mode, homepage) {
|
20 | if (mode === 'production' && homepage) {
|
21 | return /\/$/.test(homepage) ? homepage : (homepage + '/')
|
22 | }
|
23 | return '/'
|
24 | }
|
25 |
|
26 | exports.readPkg = function () {
|
27 | try {
|
28 | return require(exports.cwd('package.json'))
|
29 | } catch (err) {
|
30 | if (err.code === 'MODULE_NOT_FOUND') {
|
31 | return {}
|
32 | }
|
33 | throw new AppError(err.message)
|
34 | }
|
35 | }
|
36 |
|
37 | exports.inferHTML = function (options) {
|
38 | const result = {
|
39 | title: 'Poi App',
|
40 | template: exports.ownDir('lib/index.ejs')
|
41 | }
|
42 |
|
43 | const pkg = exports.readPkg()
|
44 | result.pkg = pkg
|
45 | result.title = pkg.productName || pkg.name
|
46 | result.description = pkg.description
|
47 |
|
48 | const templatePath = path.resolve(options.cwd || process.cwd(), 'index.ejs')
|
49 | if (fs.existsSync(templatePath)) {
|
50 | console.log(`> Using external HTML template file`)
|
51 | console.log(chalk.dim(`> location: "${tildify(templatePath)}"`))
|
52 | result.template = templatePath
|
53 | }
|
54 |
|
55 | return result
|
56 | }
|
57 |
|
58 | exports.getFileNames = function (useHash, customFileName) {
|
59 | return Object.assign({
|
60 | js: useHash ? '[name].[chunkhash:8].js' : '[name].js',
|
61 | css: useHash ? '[name].[contenthash:8].css' : '[name].css',
|
62 | static: useHash ? 'static/[name].[hash:8].[ext]' : 'static/[name].[ext]',
|
63 | chunk: useHash ? '[id].[chunkhash:8].chunk.js' : '[id].chunk.js'
|
64 | }, customFileName)
|
65 | }
|
66 |
|
67 | exports.inferProductionValue = function (value, mode) {
|
68 | if (typeof value !== 'undefined') {
|
69 | return value
|
70 | }
|
71 | return mode === 'production'
|
72 | }
|
73 |
|
74 | exports.promisify = function (fn) {
|
75 | return (...args) => {
|
76 | return new Promise((resolve, reject) => {
|
77 | fn(...args, (err, result) => {
|
78 | if (err) return reject(err)
|
79 | resolve(result)
|
80 | })
|
81 | })
|
82 | }
|
83 | }
|
84 |
|
85 | exports.stringifyObject = function (obj) {
|
86 | return Object.keys(obj).reduce((curr, next) => {
|
87 | curr[next] = JSON.stringify(obj[next])
|
88 | return curr
|
89 | }, {})
|
90 | }
|
91 |
|
92 | exports.parsePresets = presets => {
|
93 | const loadPreset = (name, options) => {
|
94 | if (typeof name === 'string') {
|
95 | let preset
|
96 | try {
|
97 | preset = /^(\.|\/)/.test(name) ? name : `poi-preset-${name}`
|
98 | name = require(path.join(process.cwd(), preset))
|
99 | } catch (err) {
|
100 | if (err.code === 'MODULE_NOT_FOUND' && err.message.indexOf(name) > -1) {
|
101 | throw new AppError(`Cannot find module "${preset}" in current working directory!\n\nYou may need to run: yarn add ${preset} --dev`)
|
102 | } else {
|
103 | throw err
|
104 | }
|
105 | }
|
106 | }
|
107 | if (typeof name === 'function') {
|
108 | name = name(options)
|
109 | }
|
110 | return name
|
111 | }
|
112 |
|
113 | presets = Array.isArray(presets) ?
|
114 | presets :
|
115 | typeof presets === 'object' ?
|
116 | Object.keys(presets).map(name => [name, presets[name]]) :
|
117 | [presets]
|
118 |
|
119 | return presets.map(preset => {
|
120 | if (typeof preset === 'string') {
|
121 | preset = loadPreset(preset)
|
122 | } else if (Array.isArray(preset)) {
|
123 | preset = loadPreset(preset[0], preset[1])
|
124 | }
|
125 |
|
126 | return preset
|
127 | })
|
128 | }
|