Skip to content

Commit 8ba7589

Browse files
committed
first version of fast-cli
0 parents  commit 8ba7589

40 files changed

+4438
-0
lines changed

.babelrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": ["es2015", "stage-2"],
3+
"plugins": [
4+
"syntax-async-functions",
5+
"transform-regenerator",
6+
[
7+
"transform-runtime",
8+
{
9+
"polyfill": false,
10+
"regenerator": true
11+
}
12+
]
13+
]
14+
}

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "orionsoft"
3+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
yarn-error.log
3+
/build
4+
*.TODO

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.*
2+
src/
3+
coverage/

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "fast",
3+
"version": "0.1.0",
4+
"main": "index.js",
5+
"author": "Robert Zibert",
6+
"license": "MIT",
7+
"bin": {
8+
"fast": "./build/index.js"
9+
},
10+
"scripts": {
11+
"build": "rm -rf ./build && babel ./src --out-dir ./build",
12+
"prepare": "yarn run build",
13+
"watch": "rm -rf ./build && babel ./src --watch --out-dir ./build",
14+
"test": "exit 0"
15+
},
16+
"dependencies": {
17+
"babel-core": "^6.26.3",
18+
"babel-plugin-source-map-support": "^2.0.1",
19+
"chokidar": "^2.0.4",
20+
"colors": "^1.2.1",
21+
"commander": "^2.15.1",
22+
"globby": "^8.0.1",
23+
"hygen": "^1.6.2",
24+
"lodash": "^4.17.5",
25+
"request": "^2.88.0",
26+
"request-promise": "^4.2.2",
27+
"walk-sync": "^0.3.2"
28+
},
29+
"devDependencies": {
30+
"babel-cli": "^6.26.0",
31+
"babel-plugin-stack-trace-sourcemap": "^1.0.2",
32+
"babel-plugin-syntax-async-functions": "^6.13.0",
33+
"babel-plugin-transform-regenerator": "^6.26.0",
34+
"babel-plugin-transform-runtime": "^6.23.0",
35+
"babel-preset-es2015": "^6.24.1",
36+
"babel-preset-stage-2": "^6.24.1",
37+
"eslint-config-orionsoft": "^1.2.8"
38+
},
39+
"publishConfig": {
40+
"access": "public"
41+
}
42+
}

src/build/compile.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import execute from '../helpers/execute'
2+
import globby from 'globby'
3+
import colors from 'colors/safe'
4+
import path from 'path'
5+
import compileFile from '../start/compileFile'
6+
7+
export default async function(dirPath) {
8+
const finalDirPath = path.join(dirPath, 'app')
9+
await execute(`rm -rf ${finalDirPath}`)
10+
const files = await globby('app/**/*')
11+
try {
12+
await Promise.all(files.map(file => compileFile(file, finalDirPath)))
13+
return true
14+
} catch (error) {
15+
await execute(`rm -rf ${finalDirPath}`)
16+
console.log(colors.red(`=> Syntax error at ${error.message}`))
17+
if (error._babel) {
18+
console.log(error.codeFrame)
19+
} else {
20+
console.error(colors.red(error))
21+
}
22+
return false
23+
}
24+
}

src/build/copyFiles.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from 'fs'
2+
3+
export default function(dirPath) {
4+
const filesToCopy = ['.npmrc']
5+
6+
for (const fileName of filesToCopy) {
7+
const path = `./${fileName}`
8+
if (!fs.existsSync(path)) continue
9+
const newPath = `${dirPath}/${fileName}`
10+
fs.createReadStream(path).pipe(fs.createWriteStream(newPath))
11+
}
12+
}

src/build/createPackageJSON.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs from 'fs'
2+
import writeFile from '../helpers/writeFile'
3+
4+
export default function(dirPath) {
5+
const packageJSON = JSON.parse(fs.readFileSync('./package.json').toString())
6+
delete packageJSON.devDependencies
7+
8+
packageJSON.scripts = {
9+
...packageJSON.scripts,
10+
start: 'node app/index.js'
11+
}
12+
13+
writeFile(`${dirPath}/package.json`, JSON.stringify(packageJSON, null, 2))
14+
}

src/build/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import compile from './compile'
2+
import os from 'os'
3+
import createPackageJSON from './createPackageJSON'
4+
import colors from 'colors/safe'
5+
import copyFiles from './copyFiles'
6+
7+
export default async function({output}) {
8+
if (!output) {
9+
throw new Error('Output dir is required')
10+
}
11+
12+
const finalDirPath = output.replace('~', os.homedir())
13+
console.log(colors.bold('Compiling your app...'))
14+
await compile(finalDirPath)
15+
createPackageJSON(finalDirPath)
16+
copyFiles(finalDirPath)
17+
console.log(colors.bold('Build created'))
18+
}

src/create/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import execute from '../helpers/execute'
2+
3+
export default async function({name, kit}) {
4+
if (!name) {
5+
throw new Error('Please set the name of the app')
6+
}
7+
if (!kit) {
8+
throw new Error('Please select which kit to use')
9+
}
10+
const repo = `https://github.com/orionjs/boilerplate-${kit}`
11+
console.log('Downloading starter kit...')
12+
await execute(`git clone ${repo} ${name}`)
13+
await execute(`cd ${name} && rm -rf .git`)
14+
console.log('Your starter kit is ready')
15+
await execute(`cd ${name} && rm -rf README.md`)
16+
}

src/handleErrors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import colors from 'colors'
2+
3+
process
4+
.on('unhandledRejection', (reason, promise) => {
5+
console.error(colors.red(reason), colors.red('Unhandled promise rejection'))
6+
})
7+
.on('uncaughtException', error => {
8+
console.error(colors.red(error))
9+
process.exit(1)
10+
})

src/helpers/checkFast.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
const checkIsFastProject = function() {
5+
const fastDirname = path.dirname()
6+
if (fs.existsSync(fastDirname)) return true
7+
return false
8+
}
9+
10+
export default checkIsFastProject

src/helpers/checkQuasarVersion.js

Whitespace-only changes.

src/helpers/checkVersion.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import rp from 'request-promise'
2+
3+
export default async function() {
4+
try {
5+
const url = 'http://registry.npmjs.org/@orion-js/cli'
6+
const response = await rp({
7+
uri: url,
8+
method: 'GET',
9+
json: true,
10+
timeout: 2000
11+
})
12+
const pjson = require('../../package.json')
13+
14+
const latestVersion = response['dist-tags'].latest
15+
const currentVersion = pjson.version
16+
17+
if (currentVersion !== latestVersion) {
18+
console.log('You are running an outdated version of the orionjs cli')
19+
console.log('Please upgrade by running "yarn global upgrade @orion-js/cli"\n')
20+
}
21+
} catch (error) {}
22+
}

src/helpers/checkVueVersion.js

Whitespace-only changes.

src/helpers/ensureDirectory.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
const ensureDirExistence = function(filePath) {
5+
const dirname = path.dirname(filePath)
6+
if (fs.existsSync(dirname)) return true
7+
ensureDirExistence(dirname)
8+
fs.mkdirSync(dirname)
9+
}
10+
11+
export default ensureDirExistence

src/helpers/execute.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {exec} from 'child_process'
2+
3+
//** Execute safe a terminal command */
4+
export default async function(command) {
5+
return new Promise(function(resolve, reject) {
6+
exec(command, (error, stdout, stderr) => {
7+
if (error) {
8+
reject(new Error(error))
9+
} else {
10+
resolve({stdout, stderr})
11+
}
12+
})
13+
})
14+
}

src/helpers/getModulesToWatch.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fs from 'fs'
2+
3+
const getDirectories = function(dir) {
4+
return fs
5+
.readdirSync(dir)
6+
.map(file => `${dir}/${file}`)
7+
.filter(file => {
8+
const stats = fs.statSync(dir)
9+
return stats.isDirectory()
10+
})
11+
}
12+
13+
const getSymlinks = function() {
14+
const symlinks = []
15+
const dirs = getDirectories('./node_modules')
16+
for (const dir of dirs) {
17+
const stats = fs.statSync(dir)
18+
const isSymlink = stats.isSymbolicLink()
19+
if (isSymlink) {
20+
symlinks.push(dir)
21+
}
22+
23+
if (dir.includes('@orion-js')) {
24+
const subDirs = getDirectories(dir)
25+
for (const subDir of subDirs) {
26+
symlinks.push(subDir + '/lib')
27+
}
28+
}
29+
}
30+
31+
return symlinks
32+
}
33+
34+
export default function() {
35+
const paths = ['node_modules']
36+
const symlinks = getSymlinks()
37+
return [...paths, ...symlinks]
38+
}

src/helpers/isPortInUse.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import net from 'net'
2+
3+
export default function(port) {
4+
return new Promise(function(resolve, reject) {
5+
const tester = net
6+
.createServer()
7+
.once('error', function(err) {
8+
if (err.code !== 'EADDRINUSE') return reject(err)
9+
resolve(true)
10+
})
11+
.once('listening', function() {
12+
tester
13+
.once('close', function() {
14+
resolve(false)
15+
})
16+
.close()
17+
})
18+
.listen(port)
19+
})
20+
}

src/helpers/onExit.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const callbacks = []
2+
3+
export default function(callback) {
4+
callbacks.push(callback)
5+
}
6+
7+
const onExit = function(...args) {
8+
for (const callback of callbacks) {
9+
callback(...args)
10+
}
11+
process.exit()
12+
}
13+
14+
// catches ctrl+c event
15+
process.on('SIGINT', onExit)
16+
process.on('SIGTERM', onExit)
17+
18+
// catches terminal close
19+
process.on('SIGHUP', onExit)
20+
21+
// catches "kill pid" (for example: nodemon restart)
22+
process.on('SIGUSR1', onExit)
23+
process.on('SIGUSR2', onExit)
24+
25+
// catches uncaught exceptions
26+
process.on('uncaughtException', onExit)

src/helpers/sleep.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default time => new Promise(resolve => setTimeout(resolve, time))

src/helpers/writeFile.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fs from 'fs'
2+
import ensureDirectory from '../helpers/ensureDirectory'
3+
4+
export default async function(path, content) {
5+
ensureDirectory(path)
6+
fs.writeFileSync(path, content)
7+
}

src/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env node
2+
import program from 'commander'
3+
import init from './init'
4+
// import build from './build'
5+
import colors from 'colors/safe'
6+
// import create from './create'
7+
// import checkVersion from './helpers/checkVersion'
8+
import './handleErrors'
9+
10+
const run = function(action) {
11+
return async function(...args) {
12+
try {
13+
await checkVersion()
14+
await action(...args)
15+
} catch (e) {
16+
console.error(colors.red('Error: ' + e.message))
17+
}
18+
}
19+
}
20+
21+
program
22+
.command('init')
23+
.description('Creates a new Fast Client project')
24+
.option('--name [name]', 'Name of the project')
25+
.action(run(init))
26+
27+
// program
28+
// .command('build')
29+
// .description('Compiles an Orionjs app and exports it to a simple nodejs app')
30+
// .option('-o, --output [output]', 'Output directory')
31+
// .action(run(build))
32+
33+
// program
34+
// .command('create')
35+
// .description('Creates a new Orionjs project')
36+
// .option('--name [name]', 'Name of the project')
37+
// .option('--kit [kit]', 'Which starter kit to use')
38+
// .action(run(create))
39+
40+
program.version(require('../package.json').version, '-v --version')
41+
42+
program.parse(process.argv)
43+
44+
if (!process.argv.slice(2).length) {
45+
program.outputHelp()
46+
}

src/init/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import execute from '../helpers/execute'
2+
3+
export default async function({ name }) {
4+
if (!name) {
5+
throw new Error('Please set the name of the app')
6+
}
7+
const repo = `https://github.com/fast-platform/`
8+
await execute(`git clone ${repo} ${name}`)
9+
await execute(`cd ${name} && rm -rf .git`)
10+
console.log('Your starter kit is ready')
11+
await execute(`cd ${name} && rm -rf README.md`)
12+
}

0 commit comments

Comments
 (0)