Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Flow specific changes #213

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ignore]
.*/node_modules/documentation/*

[libs]

[include]

[options]
suppress_comment= \\(.\\|\n\\)*\\@FlowIgnore
22 changes: 16 additions & 6 deletions cmds/build.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
'use strict'

const path = require('path')

const chokidar = require('chokidar')

const build = require('../src/build')
const onError = require('../src/error-handler')
const utils = require('../src/utils')

module.exports = {
command: 'build',
desc: 'Build ready to release',
builder: {
browser: {
alias: 'b',
describe: 'Build for browser usage',
dist: {
alias: 'd',
describe: 'Build dist package',
default: true
},
lib: {
alias: 'l',
describe: 'Transpile src to lib',
default: true
},
node: {
alias: 'n',
describe: 'Build for node usage',
watch: {
alias: 'w',
describe: 'Keep watching source files for changes',
default: false
}
},
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
"license": "MIT",
"dependencies": {
"async": "^2.6.0",
"babel-loader": "^7.1.4",
"babel-preset-flow-node": "^2.0.1",
"browserify-zlib": "^0.2.0",
"chalk": "^2.3.0",
"chokidar": "^2.0.3",
"clean-documentation-theme": "^0.5.2",
"codecov": "^3.0.0",
"conventional-changelog": "^1.1.7",
Expand All @@ -41,10 +44,11 @@
"documentation": "^5.3.3",
"es6-promisify": "^5.0.0",
"eslint": "^4.13.0",
"eslint-config-aegir": "^1.0.1",
"eslint-config-aegir": "git+https://github.com/ipfs/eslint-config-aegir#flow",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to link to a released package instead of GitHub URL

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For technical or practical/policy reasons? I'd prefer not publishing temporary forks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reproducibility and not breaking stuff in the future. If Github goes down (happens strangely often) or the branch changes, it'll break the assumption that if package.json hasn't changed, we should get the same (~) dependencies. This has bitten us in the past, when cleaning up old repositories but forgetting to update some reference and cases where we want to run tests on old commits.

You can use Prelease tags in npm to publish draft-versions of packages: https://docs.npmjs.com/misc/semver#prerelease-tags

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I could do a 1.0.1-flow.1 release and it won't do any harm for anyone not specifying exactly this version?

"execa": "^0.8.0",
"filesize": "^3.5.11",
"findup-sync": "^2.0.0",
"flow-bin": "^0.71.0",
"fs-extra": "^4.0.3",
"gh-pages": "^1.1.0",
"glob": "^7.1.2",
Expand Down Expand Up @@ -75,6 +79,7 @@
"safe-buffer": "^5.1.1",
"semver": "^5.4.1",
"simple-git": "^1.85.0",
"source-map-support": "^0.5.4",
"stream-http": "^2.7.2",
"through": "^2.3.8",
"transform-loader": "^0.2.4",
Expand Down
24 changes: 5 additions & 19 deletions src/build/browser.js → src/build/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
const webpack = require('webpack')
const Uglify = require('uglify-es')
const path = require('path')
const Listr = require('listr')
const fs = require('fs-extra')
const filesize = require('filesize')
const pify = require('pify')

const clean = require('../clean')
const utils = require('../utils')
const config = require('../config/webpack')

function webpackBuild (ctx, task) {
Expand Down Expand Up @@ -59,19 +56,8 @@ function minify (ctx, task) {
})
}

const TASKS = new Listr([{
title: 'Clean ./dist',
task: () => clean('dist')
}, {
title: 'Webpack Build',
task: webpackBuild
}, {
title: 'Write stats to disk',
task: writeStats,
enabled: (ctx) => ctx.webpackResult != null && ctx.stats
}, {
title: 'Minify',
task: minify
}], utils.getListrConfig())

module.exports = TASKS
module.exports = {
webpackBuild: webpackBuild,
writeStats: writeStats,
minify: minify
}
31 changes: 29 additions & 2 deletions src/build/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
'use strict'

const browserTasks = require('./browser')
const Listr = require('listr')

module.exports = browserTasks
const clean = require('../clean')
const dist = require('./dist')
const lib = require('./lib')
const utils = require('../utils')

const TASKS = new Listr([{
title: 'Clean ./dist',
task: () => clean('dist'),
enabled: (ctx) => ctx.dist
}, {
title: 'Webpack Build',
task: dist.webpackBuild,
enabled: (ctx) => ctx.dist
}, {
title: 'Write stats to disk',
task: dist.writeStats,
enabled: (ctx) => ctx.dist && ctx.webpackResult != null && ctx.stats
}, {
title: 'Minify',
task: dist.minify,
enabled: (ctx) => ctx.dist
}, {
title: 'Transpile src to lib',
task: lib,
enabled: (ctx) => ctx.lib
}], utils.getListrConfig())

module.exports = TASKS
109 changes: 109 additions & 0 deletions src/build/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Babel transform all *.js files from `src` to `lib`
'use strict'

const path = require('path')

const babelCore = require('babel-core')
const chokidar = require('chokidar')
const fs = require('fs-extra')

const utils = require('../utils')

const babelConfig = {
env: {
development: {
sourceMaps: 'inline',
comments: false,
presets: [
[
'env',
{
targets: {
node: 'current'
}
}
],
'flow-node'
]
}
}
}

/**
* Babel transpiles a file from `src` to `lib`
*
* @param {string} filename The filename relative to the `src` directory
*
* @returns {Promise}
*/
const babel = (filename) => {
const src = path.join('src', filename)
const dest = path.join('lib', filename)

// To have the right filename in the source map
babelConfig.sourceFileName = path.join('..', src)

return new Promise((resolve, reject) => {
babelCore.transformFile(
src, babelConfig, (err, result) => {
if (err) {
return reject(err)
}
resolve(result.code)
})
}).then((code) => {
return fs.outputFile(dest, code)
})
}

/**
* Copies a file from `src` to `lib` with flow extension
*
* @param {string} filename The filename relative to the `src` directory
*
* @returns {Promise}
*/
const flowCopy = (filename) => {
const src = path.join('src', filename)
const dest = path.join('lib', filename + '.flow')

return fs.copy(src, dest)
}

const transform = (ctx) => {
const srcDir = path.join(utils.getBasePath(), 'src')

return new Promise((resolve, reject) => {
// The watcher code is based on the babel-cli code (MIT licensed):
// https://github.com/babel/babel/blob/6597a472b30419493f123bff1e9194e4c09e488e/packages/babel-cli/src/babel/dir.js#L164-L188`
const watcher = chokidar.watch(srcDir, {
persistent: ctx.watch,
ignoreInitial: false,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10
}
})

;['add', 'change'].forEach((type) => {
watcher.on(type, (filename) => {
const relative = path.relative(srcDir, filename)
console.log('Transform file: ' + relative)
babel(relative)
flowCopy(relative)
})
})

watcher
.on('ready', () => {
// Finish the task after the initial scan. If files are watched, the
// task will keep running though.
resolve()
})
.on('error', (err) => {
reject(err)
})
})
}

module.exports = transform
7 changes: 7 additions & 0 deletions src/config/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ function webpackConfig (env) {
entry
],
devtool: sourcemap,
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}]
},
output: {
filename: path.basename(entry),
library: libraryName,
Expand Down
22 changes: 21 additions & 1 deletion src/lint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict'

const util = require('util')
const execFile = util.promisify(require('child_process').execFile)

const CLIEngine = require('eslint').CLIEngine
const flow = require('flow-bin')
const path = require('path')
const formatter = CLIEngine.getFormatter()

Expand All @@ -18,7 +22,7 @@ const FILES = [
'!**/node_modules/**'
]

function lint (opts) {
const eslint = (opts) => {
return new Promise((resolve, reject) => {
const cli = new CLIEngine({
useEslintrc: true,
Expand All @@ -37,4 +41,20 @@ function lint (opts) {
})
}

const typecheck = async (_opts) => {
try {
const { stdout } = await execFile(flow, ['check', '--color=always'])
console.log(stdout)
} catch (err) {
console.error(err.stdout)
console.error(err.stderr)
throw new Error('Type check errors')
}
}

const lint = async (opts) => {
await eslint(opts)
await typecheck(opts)
}

module.exports = lint
3 changes: 2 additions & 1 deletion src/test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function testNode (ctx) {

let args = [
'--ui', 'bdd',
'--colors'
'--colors',
'--require', 'source-map-support/register'
]

let files = [
Expand Down
2 changes: 1 addition & 1 deletion test/lint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const lint = require('../src/lint')
describe('lint', () => {
it('passes', function () {
// slow ci is slow, appveyor is even slower...
this.timeout(5000)
this.timeout(20000)
return lint({
fix: false
})
Expand Down