-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaypack.js
executable file
·169 lines (139 loc) · 5.23 KB
/
jaypack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
const figlet = require('figlet')
const chalk = require('chalk')
const inquirer = require('inquirer')
const fs = require('fs')
const { spawnSync } = require('child_process')
const projectConf = './_frontend/project.config.mjs'
var framework = ''
var projectName = ''
var projectSlug = ''
var args = process.argv.slice(2)
switch (args[0]) {
case 'install':
case 'i':
case 'ok':
if (process.argv.length == 3) {
console.log(chalk.yellow('🔨 Jaypack is fetching your dependencies and installing them.'))
run('npm', ['install', '--prefix', '_frontend'])
} else if (process.argv.length == 4) {
installNewPkg(process.argv.slice(3))
}
break
case 'watch':
case 'dev':
console.log(chalk.yellow('👀 Jaypack is watching and waiting for commands...'))
run('npm', ['run', 'watch', '--prefix', '_frontend'])
break
case 'init':
inquirer
.prompt([
{
type: 'input',
name: 'project_name',
message: "What is your project called?",
},
{
type: 'list',
name: 'project_framework',
message: 'Select your project framework:',
choices: [
'Static',
'Shopify',
'Craft'
],
}
])
.then((answers) => {
for (let [key, value] of Object.entries(answers)) {
switch(key) {
case 'project_name':
populateConfig(key, value)
break
case 'project_framework':
populateConfig(key, value)
/*
If craft:
- download jaycraft in ./_craft
- Notice about MAMP or other environment
- Create next command once environment is setup that installs craft and imports the scaffold database
If static:
- moves nunjucks scaffold into _src
If shopify
- Installs themekit
- Sets up a shopify conf file
- Downloads a shopify blank theme
*/
break
}
}
})
.catch((error) => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
break
case 'package':
console.log(chalk.yellow('📦 Runa is packaging the frontend for production'))
run('npm', ['run', 'build', '--prefix', '_frontend'])
break
case 'add':
case 'fetch':
installNewPkg(process.argv.slice(3))
break
default:
console.log(
chalk.yellow(
figlet.textSync('JayPack', { horizontalLayout: 'full' })
),
'\n\nJayPack is a frontend boilerplate to build static,\nShopify or CraftCMS projects. Below are all the\ncommands available within JayPack:\n',
chalk.yellow('\ninstall'),
'\t\t\t',
'This installs all dependencies',
chalk.yellow('\ninit'),
'\t\t\t',
'Set up the project config file & scaffold',
chalk.yellow('\nwatch'),
'\t\t\t\t',
'Start a local Browserstack instance and watch for changes',
chalk.yellow('\npackage'),
'\t\t\t',
'Compile and purge project ready for deployment',
chalk.yellow('\nfetch <package-name>'),
'\t\t',
'Fetch a new npm dependency and install it',
'\n\nSee github.com/jaybox325/jaypack for issues.\n',
)
break
}
function run(cmd, args) {
return spawnSync(cmd, args, { stdio: 'inherit' })
}
function installNewPkg(pkgs) {
let command = ['i','-D','--prefix','_frontend']
command.splice.apply(command, [2,0].concat(pkgs))
console.log(chalk.yellow(`Runa is fetching and installing ${pkgs.join(', ').replace(/, ([^,]*)$/, ' and $1')}.`))
run('npm', command)
}
function populateConfig(confType, confString) {
var result
fs.readFile(projectConf, 'utf8', function (err,data) {
if (err) {
return console.log(err)
}
switch(confType) {
case 'project_name':
result = data.replace(/name: '.*'/g, `name: '${confString.toLowerCase()}'`);
break
case 'project_framework':
result = data.replace(/framework: '.*'/g, `framework: '${confString.toLowerCase()}'`);
break
}
fs.writeFile(projectConf, result, 'utf8', function (err) {
if (err) return console.log(err);
})
})
}