Skip to content

Commit 25ec240

Browse files
author
Nick Balestra
committed
initQuestion in case that the flavor is runned locally or as a fork
1 parent 0fb974a commit 25ec240

File tree

2 files changed

+113
-104
lines changed

2 files changed

+113
-104
lines changed

packages/cycle-scripts/scripts/init.js

Lines changed: 78 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const fs = require('fs-extra')
44
const path = require('path')
55
const chalk = require('chalk')
66
const spawn = require('cross-spawn')
7-
const inquirer = require('inquirer')
8-
7+
const initQuestions = require('./initQuestions')
98
// Ask the user for which language and which stream library he want to use
109
const dependencies = {
1110
basics: [
@@ -49,36 +48,6 @@ const replacements = {
4948
}
5049
}
5150

52-
const initQuestions = [
53-
{
54-
type: 'list',
55-
name: 'language',
56-
default: 0,
57-
choices: ['JavaScript', 'TypeScript'],
58-
message: 'Which language do you want to use to write your cycle app?'
59-
},
60-
{
61-
type: 'list',
62-
name: 'streamLib',
63-
default: 0,
64-
choices: [
65-
{
66-
name: 'XStream, tailored for Cycle.js',
67-
value: 'xstream'
68-
},
69-
{
70-
name: 'Most.js, a blazing fast stream library',
71-
value: 'most'
72-
},
73-
{
74-
name: 'RxJS',
75-
value: 'rxjs'
76-
}
77-
],
78-
message: 'Which reactive stream library do you want to use?'
79-
}
80-
]
81-
8251
// function patchGitignore (appPath) {
8352
// // Rename gitignore after the fact to prevent npm from renaming it to .npmignore
8453
// // See: https://github.com/npm/npm/issues/1862
@@ -129,91 +98,96 @@ function successMsg (appName, appPath) {
12998
console.log()
13099
}
131100

132-
module.exports = function init (appPath, appName, verbose, originalDirectory) {
101+
function setup (appPath, appName, verbose, originalDirectory, options) {
133102
const ownPackageName = require(path.join(__dirname, '..', 'package.json')).name
134103
const ownPath = path.join(appPath, 'node_modules', ownPackageName)
135104
const appPackageJson = path.join(appPath, 'package.json')
136105
const appPackage = require(appPackageJson)
137106

138-
inquirer.prompt(initQuestions).then(answers => {
139-
const language = answers.language
140-
const streamLib = answers.streamLib
141-
142-
const basicDependencies = dependencies.basics
143-
const languageDependencies = dependencies.language[language]
144-
const streamLibDependencies = dependencies.streamLib[streamLib]
145-
146-
const depsToInstall = basicDependencies.concat(languageDependencies).concat(streamLibDependencies)
147-
148-
// Manipulate app's package.json
149-
// To be moved to separate module
150-
appPackage.dependencies = appPackage.dependencies || {}
151-
appPackage.devDependencies = appPackage.devDependencies || {}
152-
appPackage.scripts = {
153-
'start': 'cycle-scripts start',
154-
'test': 'cycle-scripts test',
155-
'build': 'cycle-scripts build',
156-
'eject': 'cycle-scripts eject'
157-
}
107+
const language = options.language
108+
const streamLib = options.streamLib
158109

159-
fs.writeFileSync(
160-
appPackageJson,
161-
JSON.stringify(appPackage, null, 2)
162-
)
163-
164-
// Copy flavor files
165-
// fs.copySync(path.join(ownPath, 'template'), appPath)
166-
167-
fs.ensureDirSync(path.join(appPath, 'public'))
168-
fs.copySync(path.join(ownPath, 'template/public'), path.join(appPath, 'public'))
169-
170-
// copy src and transform each of the file
171-
fs.ensureDirSync(path.join(appPath, 'src'))
172-
const templatePath = path.join(ownPath, 'template/src', language)
173-
fs.readdir(templatePath, (err, files) => {
174-
if (err) {
175-
throw err
176-
}
177-
files.forEach(file => {
178-
const targetPath = path.join(appPath, 'src', file)
179-
const fileSrc = require(path.join(templatePath, file))
180-
const targetSrc = fileSrc(replacements[streamLib])
181-
fs.outputFile(targetPath, targetSrc)
182-
})
183-
})
110+
const basicDependencies = dependencies.basics
111+
const languageDependencies = dependencies.language[language]
112+
const streamLibDependencies = dependencies.streamLib[streamLib]
113+
114+
const depsToInstall = basicDependencies.concat(languageDependencies).concat(streamLibDependencies)
115+
116+
// Manipulate app's package.json
117+
// To be moved to separate module
118+
appPackage.dependencies = appPackage.dependencies || {}
119+
appPackage.devDependencies = appPackage.devDependencies || {}
120+
appPackage.scripts = {
121+
'start': 'cycle-scripts start',
122+
'test': 'cycle-scripts test',
123+
'build': 'cycle-scripts build',
124+
'eject': 'cycle-scripts eject'
125+
}
126+
127+
fs.writeFileSync(
128+
appPackageJson,
129+
JSON.stringify(appPackage, null, 2)
130+
)
184131

185-
fs.copySync(path.join(ownPath, 'template/src', language), path.join(appPath, 'src'))
132+
// Copy flavor files
133+
// fs.copySync(path.join(ownPath, 'template'), appPath)
186134

187-
// for each file in template/src load them, replace and write them
135+
fs.ensureDirSync(path.join(appPath, 'public'))
136+
fs.copySync(path.join(ownPath, 'template/public'), path.join(appPath, 'public'))
188137

189-
// for each library
138+
// copy src and transform each of the file
139+
fs.ensureDirSync(path.join(appPath, 'src'))
140+
const templatePath = path.join(ownPath, 'template/src', language)
141+
fs.readdir(templatePath, (err, files) => {
142+
if (err) {
143+
throw err
144+
}
145+
files.forEach(file => {
146+
const targetPath = path.join(appPath, 'src', file)
147+
const fileSrc = require(path.join(templatePath, file))
148+
const targetSrc = fileSrc(replacements[streamLib])
149+
fs.outputFile(targetPath, targetSrc)
150+
})
151+
})
190152

191-
// patchGitignore(appPath)
153+
fs.copySync(path.join(ownPath, 'template/src', language), path.join(appPath, 'src'))
192154

193-
const dependecyList = depsToInstall
194-
.slice(0, (depsToInstall.length - 1))
195-
.join(', ')
196-
.concat(` and ${depsToInstall.slice(-1)}`)
155+
// for each file in template/src load them, replace and write them
197156

198-
console.log(`Installing ${dependecyList} using npm...`)
199-
console.log()
157+
// for each library
200158

201-
const args = [
202-
'install'
203-
].concat(
204-
depsToInstall
205-
).concat([
206-
'--save',
207-
verbose && '--verbose'
208-
]).filter(Boolean)
159+
// patchGitignore(appPath)
209160

210-
var proc = spawn('npm', args, {stdio: 'inherit'})
211-
proc.on('close', function (code) {
212-
if (code !== 0) {
213-
console.error(chalk.red('`npm ' + args.join(' ') + '` failed'))
214-
return
215-
}
216-
successMsg(appName, appPath)
217-
})
161+
const dependecyList = depsToInstall
162+
.slice(0, (depsToInstall.length - 1))
163+
.join(', ')
164+
.concat(` and ${depsToInstall.slice(-1)}`)
165+
166+
console.log(`Installing ${dependecyList} using npm...`)
167+
console.log()
168+
169+
const args = [
170+
'install'
171+
].concat(
172+
depsToInstall
173+
).concat([
174+
'--save',
175+
verbose && '--verbose'
176+
]).filter(Boolean)
177+
178+
var proc = spawn('npm', args, {stdio: 'inherit'})
179+
proc.on('close', function (code) {
180+
if (code !== 0) {
181+
console.error(chalk.red('`npm ' + args.join(' ') + '` failed'))
182+
return
183+
}
184+
successMsg(appName, appPath)
218185
})
219186
}
187+
188+
module.exports = function init (appPath, appName, verbose, originalDirectory, options) {
189+
if (options) {
190+
return setup(appPath, appName, verbose, originalDirectory, options)
191+
}
192+
return initQuestions(anwers => setup(appPath, appName, verbose, originalDirectory, anwers))
193+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const inquirer = require('inquirer')
4+
5+
const initQuestions = [
6+
{
7+
type: 'list',
8+
name: 'language',
9+
default: 0,
10+
choices: ['JavaScript', 'TypeScript'],
11+
message: 'Which language do you want to use to write your cycle app?'
12+
},
13+
{
14+
type: 'list',
15+
name: 'streamLib',
16+
default: 0,
17+
choices: [
18+
{
19+
name: 'XStream, tailored for Cycle.js',
20+
value: 'xstream'
21+
},
22+
{
23+
name: 'Most.js, a blazing fast stream library',
24+
value: 'most'
25+
},
26+
{
27+
name: 'RxJS',
28+
value: 'rxjs'
29+
}
30+
],
31+
message: 'Which reactive stream library do you want to use?'
32+
}
33+
]
34+
35+
module.exports = cb => inquirer.prompt(initQuestions).then(cb)

0 commit comments

Comments
 (0)