This repository has been archived by the owner on Sep 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- data sources are transpiled by default - `--no-transpile` flag is respected - a custom gateway can be supplied for dev (#10) - temporary files are removed on process exit (#16) close #11, close #10, close #16
- Loading branch information
1 parent
bb48037
commit baed286
Showing
8 changed files
with
301 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,8 @@ | ||
import { EOL } from 'os'; | ||
import yargs from 'yargs'; | ||
import cleanup from 'node-cleanup'; | ||
|
||
import { description, version } from '../package.json'; | ||
import dev from './dev'; | ||
// import cleanup from '../lib/cleanup'; | ||
|
||
yargs | ||
.command(dev) | ||
.demandCommand() | ||
// .demandCommand() | ||
.help().argv; | ||
|
||
cleanup( | ||
() => { | ||
// console.log('exiting...'); | ||
}, | ||
{ | ||
ctrl_C: `${EOL}${EOL}Thanks for using GrAMPS!`, | ||
}, | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import cpy from 'cpy'; | ||
import del from 'del'; | ||
import mkdirp from 'mkdirp'; | ||
import babel from 'babel-core'; | ||
import globby from 'globby'; | ||
import { error, log, warn } from './logger'; | ||
|
||
const TEMP_DIR = path.resolve(__dirname, '..', '.tmp'); | ||
|
||
const handleError = (err, msg, callback) => { | ||
if (!err) { | ||
return; | ||
} | ||
|
||
error(msg || err); | ||
|
||
if (callback) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
throw err; | ||
}; | ||
|
||
const getDirName = dir => | ||
dir | ||
.split('/') | ||
.filter(str => str) | ||
.slice(-1) | ||
.pop(); | ||
|
||
export const cleanUpTempDir = () => del(TEMP_DIR); | ||
|
||
const makeTempDir = tmpDir => | ||
new Promise((resolve, reject) => { | ||
mkdirp(tmpDir, err => { | ||
handleError(err, `Unable to create ${tmpDir}`, reject); | ||
log(` -> created a temporary directory at ${tmpDir}`); | ||
resolve(tmpDir); | ||
}); | ||
}); | ||
|
||
const makeParentTempDir = () => | ||
new Promise((resolve, reject) => { | ||
cleanUpTempDir().then(() => { | ||
mkdirp(TEMP_DIR, err => { | ||
handleError(err, `Could not create ${TEMP_DIR}`); | ||
resolve(TEMP_DIR); | ||
}); | ||
}); | ||
}); | ||
|
||
const isValidDataSource = dataSourcePath => { | ||
if (!fs.existsSync(dataSourcePath)) { | ||
warn(`Could not load a data source from ${dataSourcePath}`); | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const loadDataSourceFromPath = dataSourcePath => { | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
const src = require(dataSourcePath); | ||
const dataSource = src.default || src; | ||
|
||
// TODO check for required properties. | ||
|
||
log(` -> successfully loaded ${dataSource.namespace}`); | ||
|
||
return src.default || src; | ||
}; | ||
|
||
export const loadDataSources = pathArr => { | ||
return pathArr.filter(isValidDataSource).map(loadDataSourceFromPath); | ||
}; | ||
|
||
const writeTranspiledFile = ({ filename, tmpFile, transpiled }) => | ||
new Promise((resolve, reject) => { | ||
fs.writeFile(tmpFile, transpiled.code, err => { | ||
handleError(err, `Unable to transpile ${tmpFile}`, reject); | ||
resolve(filename); | ||
}); | ||
}); | ||
|
||
// For convenience, we transpile data sources using GrAMPS settings. | ||
const transpileJS = dataSource => tmpDir => | ||
new Promise((resolve, reject) => { | ||
const filePromises = globby | ||
.sync(path.join(dataSource, '{src,}/*.js')) | ||
.map(file => { | ||
const filename = path.basename(file); | ||
|
||
return { | ||
filename, | ||
tmpFile: path.join(tmpDir, filename), | ||
transpiled: babel.transformFileSync(file), | ||
}; | ||
}) | ||
.map(writeTranspiledFile); | ||
|
||
Promise.all(filePromises).then(() => resolve(tmpDir)); | ||
}); | ||
|
||
const copyGraphQLFiles = dataSource => tmpDir => | ||
new Promise((resolve, reject) => { | ||
const filePromises = globby | ||
.sync(path.join(dataSource, '{src,}/*.graphql')) | ||
.map(file => cpy(file, tmpDir)); | ||
|
||
Promise.all(filePromises).then(() => resolve(tmpDir)); | ||
}); | ||
|
||
// We have to symlink node_modules or we’ll get errors when requiring packages. | ||
const symlinkNodeModules = dataSource => tmpDir => | ||
new Promise((resolve, reject) => { | ||
fs.symlink( | ||
path.join(dataSource, 'node_modules'), | ||
path.join(tmpDir, 'node_modules'), | ||
err => { | ||
handleError(err, 'Unable to symlink the Node modules folder', reject); | ||
resolve(tmpDir); | ||
}, | ||
); | ||
}); | ||
|
||
const transpileDataSource = parentDir => dataSource => | ||
new Promise((resolve, reject) => { | ||
const dirName = getDirName(dataSource); | ||
const tmpDir = path.join(parentDir, dirName); | ||
|
||
// Make a temporary directory for the data source. | ||
makeTempDir(tmpDir) | ||
.then(transpileJS(dataSource)) | ||
.then(copyGraphQLFiles(dataSource)) | ||
.then(symlinkNodeModules(dataSource)) | ||
.then(resolve) | ||
.catch(err => handleError(err, null, reject)); | ||
}); | ||
|
||
export const transpileDataSources = (shouldTranspile, dataSources) => | ||
new Promise((resolve, reject) => { | ||
if (!shouldTranspile) { | ||
resolve(dataSources); | ||
return; | ||
} | ||
|
||
makeParentTempDir() | ||
.then(parentDir => | ||
dataSources | ||
.filter(isValidDataSource) | ||
.map(transpileDataSource(parentDir)), | ||
) | ||
.then(dataSourcePromises => { | ||
Promise.all(dataSourcePromises).then(resolve); | ||
}) | ||
.catch(reject); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { EOL } from 'os'; | ||
import chalk from 'chalk'; | ||
|
||
const padMsg = msg => | ||
[''] | ||
.concat(msg) | ||
.concat('') | ||
.join(EOL); | ||
|
||
export const error = msg => console.error(chalk.red.bold(padMsg(msg))); | ||
export const log = msg => console.log(chalk.dim(msg)); | ||
export const success = msg => console.log(chalk.green(padMsg(msg))); | ||
export const warn = msg => console.warn(chalk.yellow.bold(padMsg(msg))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.