Skip to content

Commit 6ec1be2

Browse files
author
Nick Balestra
committed
cycle-scripts reworks
1 parent 3f0de48 commit 6ec1be2

File tree

9 files changed

+268
-270
lines changed

9 files changed

+268
-270
lines changed

packages/cycle-scripts/package.json

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,20 @@
3030
"cycle-scripts": "./index.js"
3131
},
3232
"dependencies": {
33-
"anser": "^1.2.7",
34-
"babel-core": "^6.23.1",
35-
"babel-loader": "^6.4.0",
33+
"babel-core": "^6.24.1",
34+
"babel-loader": "^6.4.1",
3635
"babel-plugin-transform-object-rest-spread": "^6.23.0",
37-
"babel-plugin-transform-react-jsx": "^6.23.0",
38-
"babel-preset-env": "^1.2.2",
36+
"babel-plugin-transform-react-jsx": "^6.24.1",
37+
"babel-preset-env": "^1.3.3",
3938
"chalk": "^1.1.3",
40-
"cross-spawn": "^4.0.2",
41-
"fs-extra": "^0.30.0",
39+
"cross-spawn": "^5.1.0",
40+
"fs-extra": "^2.1.2",
4241
"html-webpack-plugin": "^2.28.0",
4342
"inquirer": "^3.0.6",
44-
"mocha": "^3.1.2",
43+
"jest": "^19.0.2",
4544
"react-dev-utils": "^0.5.2",
46-
"snabbdom-pragma": "^1.7.0",
47-
"sockjs-client": "^1.1.2",
48-
"strip-ansi": "^3.0.1",
49-
"webpack": "^2.2.1",
45+
"snabbdom-pragma": "^1.8.0",
46+
"webpack": "^2.3.3",
5047
"webpack-dev-server": "^2.4.2"
5148
}
5249
}

packages/cycle-scripts/scripts/build.js

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,29 @@ const path = require('path')
4141

4242
const buildPath = path.join(process.cwd(), 'build')
4343
const publicPath = path.join(process.cwd(), 'public')
44-
const ccaConfig = require(path.join(process.cwd(), 'package.json'))['create-cycle-app']
45-
const config = require(path.join('../configs/', ccaConfig.language, 'webpack.config.dev'))
46-
47-
// Remove all content but keep the directory so that
48-
// if you're in it, you don't end up in Trash
49-
fs.emptyDirSync(buildPath)
50-
51-
// Start the webpack build
52-
build()
53-
54-
// Merge with the public folder
55-
copyPublicFolder()
44+
const notEjected = require(path.join(process.cwd(), 'package.json')).cca
45+
46+
const FileSizeReporter = require('react-dev-utils/FileSizeReporter')
47+
const measureFileSizesBeforeBuild = FileSizeReporter.measureFileSizesBeforeBuild
48+
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild
49+
50+
const config = require(path.join(
51+
'../configs/',
52+
notEjected
53+
? notEjected.language
54+
: '',
55+
'webpack.config.prod')
56+
)
57+
58+
measureFileSizesBeforeBuild(buildPath).then(previousFileSizes => {
59+
// Remove all content but keep the directory so that
60+
// if you're in it, you don't end up in Trash
61+
fs.emptyDirSync(buildPath)
62+
// Start the webpack build
63+
build(previousFileSizes)
64+
// Merge with the public folder
65+
copyPublicFolder()
66+
})
5667

5768
// Print out errors
5869
function printErrors (summary, errors) {
@@ -73,7 +84,7 @@ function copyPublicFolder () {
7384
}
7485

7586
// Create the production build and print the deployment instructions.
76-
function build () {
87+
function build (previousFileSizes) {
7788
console.log('Creating an optimized production build...')
7889

7990
let compiler
@@ -105,50 +116,11 @@ function build () {
105116

106117
console.log(chalk.green('Compiled successfully.'))
107118
console.log()
119+
120+
console.log('File sizes after gzip:')
121+
console.log()
122+
printFileSizesAfterBuild(stats, previousFileSizes)
123+
console.log()
108124
})
125+
// todo better output
109126
}
110-
111-
// const fs = require('fs-extra')
112-
// const path = require('path')
113-
// const mkdirp = require('mkdirp')
114-
// const webpack = require('webpack')
115-
// const ProgressBarPlugin = require('progress-bar-webpack-plugin')
116-
117-
// const buildPath = path.join(process.cwd(), 'build')
118-
// const publicPath = path.join(process.cwd(), 'public')
119-
120-
// mkdirp.sync(buildPath)
121-
122-
// const compiler = webpack({
123-
// entry: [
124-
// './src/'
125-
// ],
126-
// output: {
127-
// filename: 'bundle.js',
128-
// path: './public/'
129-
// },
130-
// module: {
131-
// loaders: [
132-
// {
133-
// test: /\.js$/,
134-
// loader: 'babel',
135-
// query: {
136-
// presets: ['es2015']
137-
// },
138-
// exclude: /node_modules/
139-
// }
140-
// ]
141-
// },
142-
// plugins: [
143-
// new ProgressBarPlugin(),
144-
// new webpack.optimize.UglifyJsPlugin({minimize: true})
145-
// ]
146-
// })
147-
148-
// compiler.run((err, stats) => {
149-
// if (err) {
150-
// console.log(err)
151-
// } else {
152-
// fs.copySync(publicPath, buildPath)
153-
// }
154-
// })

packages/cycle-scripts/scripts/eject.js

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,77 @@ const path = require('path')
55
const chalk = require('chalk')
66
const inquirer = require('inquirer')
77

8-
const scriptsPath = path.join(process.cwd(), 'scripts')
9-
const ownPackageJsonPath = path.resolve(__dirname, '..', 'package.json')
10-
const appPackageJsonPath = path.join(process.cwd(), 'package.json')
11-
const ownPackageJson = JSON.parse(fs.readFileSync(ownPackageJsonPath))
12-
const appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath))
8+
const appPath = path.join(process.cwd())
9+
const appScriptsPath = path.join(process.cwd(), 'scripts')
10+
const flavorPackageJson = require(path.join(__dirname, '..', 'package.json'))
11+
const appPackageJson = require(path.join(appPath, 'package.json'))
12+
const language = appPackageJson.cca.language
1313

14-
// Ask the user for confirmation before ejecting.
15-
// Abort in case of negative answer (default)
1614
const ejectConfirmation = {
1715
type: 'confirm',
1816
name: 'doEject',
1917
default: false,
2018
message: 'Are you sure you want to eject? This action is permanent.'
2119
}
2220

21+
// Ask the user for confirmation before ejecting.
2322
inquirer.prompt([ejectConfirmation]).then(answers => {
24-
if (!answers.doEject.value) {
23+
// Abort in case of negative answer (default)
24+
if (answers.doEject.value) {
2525
console.log(chalk.cyan('Eject aborted!'))
26-
return
26+
process.exit(0)
2727
}
28-
})
29-
30-
// Declaring new scripts
31-
const scripts = {
32-
start: 'node scripts/start.js',
33-
test: 'node scripts/test.js',
34-
build: 'node scripts/build.js'
35-
}
36-
37-
// Declare the new dependencies, excluding self
38-
let devDependencies = {}
39-
Object.keys(appPackageJson.devDependencies)
40-
.filter(dep => { return dep !== ownPackageJson.name })
41-
.forEach(dep => {
42-
devDependencies[dep] = appPackageJson.devDependencies[dep]
43-
})
44-
devDependencies = Object.assign({}, devDependencies, ownPackageJson.dependencies)
4528

46-
// Write the new package.json
47-
const newPackageJson = Object.assign({}, appPackageJson, {scripts: scripts, devDependencies: devDependencies})
48-
fs.writeFileSync(
49-
appPackageJsonPath,
50-
JSON.stringify(newPackageJson, null, 2)
51-
)
52-
53-
// Copy scripts
54-
function copyScript (script) {
55-
fs.copySync(path.join(__dirname, script), path.join(scriptsPath, script))
56-
}
29+
// STEP 1 - Prepare package.json
30+
// Declaring new scripts
31+
const scripts = {
32+
start: 'node scripts/start.js',
33+
test: 'node scripts/test.js',
34+
build: 'node scripts/build.js'
35+
}
36+
// Remove flavor from devpendencies
37+
delete appPackageJson.devDependencies[flavorPackageJson.name]
38+
// Remove cca settings
39+
delete appPackageJson.cca
40+
// Flavor's dependencies -> application devDependency.
41+
// We merge flavor's dependencies with application's devDepependencies
42+
const devDependencies = Object.assign(
43+
{},
44+
appPackageJson.devDependencies,
45+
flavorPackageJson.dependencies
46+
)
47+
// New package.json content
48+
const packageJsonContent = Object.assign(
49+
{},
50+
appPackageJson,
51+
{
52+
scripts,
53+
devDependencies
54+
}
55+
)
56+
// Overide application's package.json with the new content
57+
fs.writeFileSync(
58+
path.join(appPath, 'package.json'),
59+
JSON.stringify(packageJsonContent, null, 2)
60+
)
5761

58-
fs.ensureDirSync(scriptsPath)
59-
copyScript('start.js')
60-
copyScript('test.js')
61-
copyScript('build.js')
62+
// STEP 2 - Copy scripts
63+
function copyScript (script) {
64+
fs.copySync(path.join(__dirname, script), path.join(appScriptsPath, script))
65+
}
66+
// Make sure appScriptsPath exists
67+
fs.ensureDirSync(appScriptsPath)
68+
// Copy over start, test and build scripts
69+
copyScript('start.js')
70+
copyScript('test.js')
71+
copyScript('build.js')
6272

63-
// Copy utils
64-
fs.copySync(path.join(__dirname, 'utils'), path.join(scriptsPath, 'utils'))
73+
// STEP 3 - Copy utils
74+
fs.copySync(path.join(__dirname, 'utils'), path.join(appScriptsPath, 'utils'))
6575

66-
// Copy configs
67-
fs.copySync(path.join(__dirname, '../', 'configs'), path.join('configs'))
76+
// STEP 4 - Copy configs
77+
fs.copySync(path.join(__dirname, '../', 'configs', language), path.join(appPath, 'configs'))
78+
fs.copySync(path.join(__dirname, '../', 'configs', 'webpackDevServer.config.js'), path.join(appPath, 'configs', 'webpackDevServer.config.js'))
6879

69-
// Todo: provide some success info on success
80+
// TODO sucess message
81+
})

0 commit comments

Comments
 (0)