Skip to content

Commit

Permalink
Move build automation scripts into the /scripts folder
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
koistya committed Nov 29, 2016
1 parent 63bc511 commit 248864a
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 107 deletions.
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ You can use it either just as a playground or a base for your next API project.

## Directory Layout

```sh
```bash
.
├── /build/ # The compiled output (via Babel)
├── /node_modules/ # Project dependencies (npm modules)
├── /scripts/ # Build automation scripts
├── /src/ # Node.js application source files
│ ├──/types/ # GraphQL types /w resolve functions
│ │ ├── /User.js # User account (id, email, etc.)
Expand All @@ -29,21 +30,20 @@ You can use it either just as a playground or a base for your next API project.
│ ├── /schema.js # GraphQL schema
│ └── /server.js # Node.js server (entry point)
├── /test/ # Unit and integration tests
├── package.json # The list of project dependencies
└── run.js # Build automation scripts
└── package.json # The list of project dependencies
```


## Getting Started

Just clone the repo and start hacking:

```sh
$ git clone -o graphql-starter-kit -b master --single-branch \
https://github.com/kriasoft/graphql-starter-kit.git api.example.com
$ cd api.example.com
$ npm install
$ npm start
```bash
git clone -o graphql-starter-kit -b master --single-branch \
https://github.com/kriasoft/graphql-starter-kit.git api.example.com
cd api.example.com
npm install
npm start # Alternatively, node scripts/start.js
```

The GraphQL server should become available at [http://localhost:5000/](http://localhost:5000/)
Expand All @@ -54,17 +54,18 @@ The GraphQL server should become available at [http://localhost:5000/](http://lo

If you need to build the project without launching a dev server:

```sh
$ npm run build # Compiles the app into the /build folder
$ npm run build:watch # Compiles the app and starts watching for changes
```bash
node scripts/build.js # Compiles the app into the /build folder
node scripts/build.js --watch # Compiles the app and starts watching for changes
```


## How to Test

```sh
$ npm run test # Run unit tests once
$ npm run test:watch # Run unit tests in watch mode
```bash
npm run lint # Find problematic patterns in code
npm run test # Run unit tests once
npm run test:watch # Run unit tests in watch mode
```


Expand All @@ -74,15 +75,15 @@ Pick one of the two ways of launching the Node.js app in a debug mode:

#### Option #1

```sh
$ npm run build
$ node --debug --nolazy build/server
```bash
node scripts/build.js --watch
node build/server.js --debug --nolazy
```

#### Option #2

```sh
$ npm start -- --debug --nolazy
```bash
node scripts/start.js --debug --nolazy
```

Then attach your debugger to the process listening on `127.0.0.1:5858` ([learn more](https://code.visualstudio.com/Docs/editor/debugging)).
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"eslint-config-airbnb-base": "^10.0.1",
"eslint-plugin-import": "^2.2.0",
"mocha": "^3.2.0",
"nodemon": "^1.11.0"
"nodemon": "^1.11.0",
"rimraf": "^2.5.4"
},
"babel": {
"plugins": [
Expand All @@ -46,11 +47,11 @@
"extends": "airbnb-base"
},
"scripts": {
"lint": "eslint src test",
"test": "mocha --compilers js:babel-core/register",
"test:watch": "mocha --compilers js:babel-core/register --reporter min --watch",
"build": "node run build",
"build:watch": "node run build --watch",
"start": "node run start"
"lint": "eslint src test scripts --ignore-pattern scripts/start.js",
"test": "mocha test/unit --compilers js:babel-core/register",
"test:watch": "mocha test/unit --compilers js:babel-core/register --reporter min --watch",
"build": "node scritps/build",
"build:watch": "node scripts/build --watch",
"start": "node scripts/start"
}
}
79 changes: 0 additions & 79 deletions run.js

This file was deleted.

5 changes: 5 additions & 0 deletions scripts/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }]
}
}
44 changes: 44 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* GraphQL Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

const fs = require('fs');
const cp = require('child_process');
const pkg = require('../package.json');
const clean = require('./clean');
const task = require('./lib/task');

module.exports = task(() => Promise.resolve()
.then(clean)
.then(() => new Promise((resolve) => {
cp.spawn('node', [
'node_modules/babel-cli/bin/babel.js',
'src',
'--out-dir',
'build',
'--source-maps',
...(process.argv.includes('--watch') || process.argv.includes('-w') ? ['--watch'] : []),
], { stdio: ['inherit', 'pipe', 'inherit'] })
.on('exit', resolve)
.stdout.on('data', (data) => {
if (data.toString().startsWith('src/server.js')) {
const src = fs.readFileSync('build/server.js', 'utf8');
fs.writeFileSync('build/server.js', `require('source-map-support').install(); ${src}`, 'utf8');
}
process.stdout.write(data);
});
}))
.then(() => new Promise((resolve) => {
fs.writeFileSync('build/package.json', JSON.stringify({
engines: pkg.engines,
dependencies: pkg.dependencies,
scripts: { start: 'node server.js' },
}, null, ' '), 'utf8');
process.stdout.write('package.json -> build/package.json\n');
resolve();
})));
22 changes: 22 additions & 0 deletions scripts/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* GraphQL Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

const rimraf = require('rimraf');
const task = require('./lib/task');

module.exports = task(() => new Promise((resolve, reject) => {
rimraf('build/*', {
nosort: true,
dot: true,
ignore: ['build/.git'],
}, (err) => {
if (err) reject(err);
resolve();
});
}));
25 changes: 25 additions & 0 deletions scripts/lib/task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* GraphQL Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

const path = require('path');

function run(task, action) {
const start = new Date();
process.stdout.write(`Starting '${task}'...\n`);
return Promise.resolve().then(() => action()).then(() => {
process.stdout.write(`Finished '${task}' after ${new Date().getTime() - start.getTime()}ms\n`);
}, err => process.stderr.write(`${err.stack}\n`));
}

module.exports = (action) => {
const task = path.basename(module.parent.filename, '.js');
return run.bind(undefined, task, action);
};

process.nextTick(() => require.main.exports());
33 changes: 33 additions & 0 deletions scripts/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* GraphQL Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

const path = require('path');
const nodemon = require('nodemon');
require('./lib/task');
const config = require('../src/config');

module.exports = () => {
process.stdout.write('\033c');
nodemon({
watch: [path.join(__dirname, 'src')],
ignore: [],
verbose: true,
script: 'src/server.js',
nodeArgs: ['--require', 'babel-register', ...process.argv.filter(x => x.startsWith('-'))],
stdout: false,
})
.on('stdout', data => {
if (data.toString().includes(config.message)) {
process.stdout.write('\033c');
}
process.stdout.write(data);
})
.on('stderr', data => process.stderr.write(data))
.on('start', () => process.stdout.write('\033cBuilding...'));
};
2 changes: 1 addition & 1 deletion test/ViewerSpec.js → test/unit/ViewerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import app from '../src/app';
import app from '../../src/app';

chai.use(chaiHttp);

Expand Down

0 comments on commit 248864a

Please sign in to comment.