Skip to content

Commit

Permalink
Setup cleanup script (#2)
Browse files Browse the repository at this point in the history
* Implement removal of lines of file that will be removed

* Implement removal of example files and cleanup script

* Add info about the `cleanup` npm script to README
  • Loading branch information
talyssonoc authored Apr 7, 2017
1 parent ecd937f commit 1e319db
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ It's an opinionated boilerplate for Node web APIs focused on separation of conce

## Quick start

_Notice that the boilerplate comes with a small application for user management already, you can delete it after you understand how the boilerplate works but please do the quick start first!_ 😊
_Notice that the boilerplate comes with a small application for user management already, you can delete it with a npm script after you understand how the boilerplate works but please do the quick start first!_ 😊

1. Clone the repository with `git clone --depth=1 https://github.com/talyssonoc/node-api-boilerplate`
2. Setup the database on `config/database.js` (there's an example file there to be used with PostgreSQL 😉 )
Expand All @@ -85,7 +85,8 @@ This boilerplate comes with a collection of npm scripts to make your life easier
- `coverage`: Run the test suite and generate code coverage, the output will be on `coverage` folder
- `lint`: Run the linter
- `sequelize`: Alias to use the [Sequelize CLI](https://github.com/sequelize/cli)
- `console`: Open a pre-built console, you can access the DI container through the `container` variable once it's open, the console is promise-friendly
- `console`: Open a pre-built console, you can access the DI container through the `container` variable once it's open, the console is promise-friendly
- `cleanup`: Removes the files from example application

## Tech

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"sequelize": "cross-env NODE_PATH=. sequelize",
"console": "cross-env NODE_PATH=. node src/interfaces/console/index.js",
"heroku-postbuild": "NODE_ENV=production NODE_PATH=. sequelize db:migrate --url=$DATABASE_URL",
"pm2": "pm2"
"pm2": "pm2",
"cleanup": "node scripts/cleanup.js"
},
"repository": {},
"author": "Talysson <talyssonoc@gmail.com>",
Expand All @@ -27,6 +28,7 @@
"compression": "^1.6.2",
"cors": "^2.8.1",
"cross-env": "^3.2.3",
"del": "^2.2.2",
"dotenv": "^4.0.0",
"express": "^4.15.2",
"express-status-monitor": "^0.1.9",
Expand All @@ -50,6 +52,7 @@
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"nodemon": "^1.11.0",
"replace-in-file": "^2.5.0",
"supertest": "^3.0.0",
"supertest-as-promised": "^4.0.2"
}
Expand Down
53 changes: 53 additions & 0 deletions scripts/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const path = require('path');
const replace = require('replace-in-file').sync;
const remove = require('del').sync;

const srcPath = path.join(__dirname, '..', 'src');
const testPath = path.join(__dirname, '..', 'test');
const srcAndTestPath = `{${testPath},${srcPath}}`;
const routerPath = path.join(srcPath, 'interfaces', 'http', 'router.js');
const containerPath = path.join(srcPath, 'container.js');

// Remove the references of the files that will be removed

replace({
files: routerPath,
from: /\s*apiRouter.*UsersController'\)\);/,
to: ''
});

replace({
files: containerPath,
from: [
/\s*const.*app\/user'\);/,
/\s*const.*UsersRepository'\);/,
/\, User: UserModel/,
/\s*usersRepository.*\}\]/,
/\,\s*UserModel/,
/createUser.*\n/,
/\s*getAllUsers.*GetAllUsers/,
],
to: ''
});

// Remove example app files

remove([
path.join(srcAndTestPath, 'app', 'user', '**'),
path.join(srcAndTestPath, 'domain', 'user', '**'),
path.join(srcAndTestPath, 'infra', 'user', '**'),
path.join(srcPath, 'infra', 'database', 'migrate', '*.js'),
path.join(srcPath, 'infra', 'database', 'seeds', '*.js'),
path.join(srcPath, 'infra', 'database', 'models', 'User.js'),
path.join(srcPath, 'interfaces', 'http', 'user', '**'),
path.join(testPath, 'api', 'users', '**'),
path.join(testPath, 'support', 'factories', '*.js')
]);

// Remove `cleanup` command from package.json

replace({
files: path.join(__dirname, '..', 'package.json'),
from: /\,\s*\"cleanup.*cleanup\.js\"/,
to: ''
});
9 changes: 9 additions & 0 deletions src/interfaces/http/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ module.exports = ({ config, containerMiddleware, loggerMiddleware, errorHandler
.use(compression())
.use(containerMiddleware);

/*
* Add your API routes here
*
* You can use the `controllers` helper like this:
* apiRouter.use('/users', controller(controllerPath))
*
* The `controllerPath` is relative to the `interfaces/http` folder
*/

apiRouter.use('/users', controller('user/UsersController'));

router.use('/api', apiRouter);
Expand Down
Empty file added test/api/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const cleanDatabase = require('test/support/cleanDatabase');
chai.use(dirtyChai);
chai.use(chaiChange);

// Comment or remove this line if you're not using a database
// Comment this line if you're not using a database
beforeEach(cleanDatabase);
2 changes: 1 addition & 1 deletion test/support/cleanDatabase.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const container = require('src/container');
const database = container.resolve('database');

module.exports = () => database.truncate({ cascade: true });
module.exports = () => database && database.truncate({ cascade: true });
Empty file added test/support/factories/.gitkeep
Empty file.
34 changes: 33 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ defaults@^1.0.0:
dependencies:
clone "^1.0.2"

del@^2.0.2:
del@^2.0.2, del@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/del/-/del-2.2.2.tgz#c12c981d067846c84bcaf862cff930d907ffd1a8"
dependencies:
Expand Down Expand Up @@ -3221,6 +3221,14 @@ replace-ext@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/replace-ext/-/replace-ext-0.0.1.tgz#29bbd92078a739f0bcce2b4ee41e837953522924"

replace-in-file@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/replace-in-file/-/replace-in-file-2.5.0.tgz#1f6388225c8acb4db288a8189365e266f9824da4"
dependencies:
chalk "^1.1.3"
glob "^7.1.1"
yargs "^7.0.1"

request@^2.79.0:
version "2.80.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.80.0.tgz#8cc162d76d79381cdefdd3505d76b80b60589bd0"
Expand Down Expand Up @@ -4128,6 +4136,12 @@ yargs-parser@^4.2.0:
dependencies:
camelcase "^3.0.0"

yargs-parser@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-5.0.0.tgz#275ecf0d7ffe05c77e64e7c86e4cd94bf0e1228a"
dependencies:
camelcase "^3.0.0"

yargs@^6.5.0:
version "6.6.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-6.6.0.tgz#782ec21ef403345f830a808ca3d513af56065208"
Expand All @@ -4146,6 +4160,24 @@ yargs@^6.5.0:
y18n "^3.2.1"
yargs-parser "^4.2.0"

yargs@^7.0.1:
version "7.0.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-7.0.2.tgz#115b97df1321823e8b8648e8968c782521221f67"
dependencies:
camelcase "^3.0.0"
cliui "^3.2.0"
decamelize "^1.1.1"
get-caller-file "^1.0.1"
os-locale "^1.4.0"
read-pkg-up "^1.0.1"
require-directory "^2.1.1"
require-main-filename "^1.0.1"
set-blocking "^2.0.0"
string-width "^1.0.2"
which-module "^1.0.0"
y18n "^3.2.1"
yargs-parser "^5.0.0"

yargs@~3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"
Expand Down

0 comments on commit 1e319db

Please sign in to comment.