Skip to content

Commit 1e319db

Browse files
authored
Setup cleanup script (#2)
* 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
1 parent ecd937f commit 1e319db

File tree

9 files changed

+104
-6
lines changed

9 files changed

+104
-6
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ It's an opinionated boilerplate for Node web APIs focused on separation of conce
5858

5959
## Quick start
6060

61-
_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!_ 😊
61+
_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!_ 😊
6262

6363
1. Clone the repository with `git clone --depth=1 https://github.com/talyssonoc/node-api-boilerplate`
6464
2. Setup the database on `config/database.js` (there's an example file there to be used with PostgreSQL 😉 )
@@ -85,7 +85,8 @@ This boilerplate comes with a collection of npm scripts to make your life easier
8585
- `coverage`: Run the test suite and generate code coverage, the output will be on `coverage` folder
8686
- `lint`: Run the linter
8787
- `sequelize`: Alias to use the [Sequelize CLI](https://github.com/sequelize/cli)
88-
- `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
88+
- `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
89+
- `cleanup`: Removes the files from example application
8990

9091
## Tech
9192

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"sequelize": "cross-env NODE_PATH=. sequelize",
1616
"console": "cross-env NODE_PATH=. node src/interfaces/console/index.js",
1717
"heroku-postbuild": "NODE_ENV=production NODE_PATH=. sequelize db:migrate --url=$DATABASE_URL",
18-
"pm2": "pm2"
18+
"pm2": "pm2",
19+
"cleanup": "node scripts/cleanup.js"
1920
},
2021
"repository": {},
2122
"author": "Talysson <talyssonoc@gmail.com>",
@@ -27,6 +28,7 @@
2728
"compression": "^1.6.2",
2829
"cors": "^2.8.1",
2930
"cross-env": "^3.2.3",
31+
"del": "^2.2.2",
3032
"dotenv": "^4.0.0",
3133
"express": "^4.15.2",
3234
"express-status-monitor": "^0.1.9",
@@ -50,6 +52,7 @@
5052
"istanbul": "^0.4.5",
5153
"mocha": "^3.2.0",
5254
"nodemon": "^1.11.0",
55+
"replace-in-file": "^2.5.0",
5356
"supertest": "^3.0.0",
5457
"supertest-as-promised": "^4.0.2"
5558
}

scripts/cleanup.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const path = require('path');
2+
const replace = require('replace-in-file').sync;
3+
const remove = require('del').sync;
4+
5+
const srcPath = path.join(__dirname, '..', 'src');
6+
const testPath = path.join(__dirname, '..', 'test');
7+
const srcAndTestPath = `{${testPath},${srcPath}}`;
8+
const routerPath = path.join(srcPath, 'interfaces', 'http', 'router.js');
9+
const containerPath = path.join(srcPath, 'container.js');
10+
11+
// Remove the references of the files that will be removed
12+
13+
replace({
14+
files: routerPath,
15+
from: /\s*apiRouter.*UsersController'\)\);/,
16+
to: ''
17+
});
18+
19+
replace({
20+
files: containerPath,
21+
from: [
22+
/\s*const.*app\/user'\);/,
23+
/\s*const.*UsersRepository'\);/,
24+
/\, User: UserModel/,
25+
/\s*usersRepository.*\}\]/,
26+
/\,\s*UserModel/,
27+
/createUser.*\n/,
28+
/\s*getAllUsers.*GetAllUsers/,
29+
],
30+
to: ''
31+
});
32+
33+
// Remove example app files
34+
35+
remove([
36+
path.join(srcAndTestPath, 'app', 'user', '**'),
37+
path.join(srcAndTestPath, 'domain', 'user', '**'),
38+
path.join(srcAndTestPath, 'infra', 'user', '**'),
39+
path.join(srcPath, 'infra', 'database', 'migrate', '*.js'),
40+
path.join(srcPath, 'infra', 'database', 'seeds', '*.js'),
41+
path.join(srcPath, 'infra', 'database', 'models', 'User.js'),
42+
path.join(srcPath, 'interfaces', 'http', 'user', '**'),
43+
path.join(testPath, 'api', 'users', '**'),
44+
path.join(testPath, 'support', 'factories', '*.js')
45+
]);
46+
47+
// Remove `cleanup` command from package.json
48+
49+
replace({
50+
files: path.join(__dirname, '..', 'package.json'),
51+
from: /\,\s*\"cleanup.*cleanup\.js\"/,
52+
to: ''
53+
});

src/interfaces/http/router.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ module.exports = ({ config, containerMiddleware, loggerMiddleware, errorHandler
2828
.use(compression())
2929
.use(containerMiddleware);
3030

31+
/*
32+
* Add your API routes here
33+
*
34+
* You can use the `controllers` helper like this:
35+
* apiRouter.use('/users', controller(controllerPath))
36+
*
37+
* The `controllerPath` is relative to the `interfaces/http` folder
38+
*/
39+
3140
apiRouter.use('/users', controller('user/UsersController'));
3241

3342
router.use('/api', apiRouter);

test/api/.gitkeep

Whitespace-only changes.

test/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ const cleanDatabase = require('test/support/cleanDatabase');
66
chai.use(dirtyChai);
77
chai.use(chaiChange);
88

9-
// Comment or remove this line if you're not using a database
9+
// Comment this line if you're not using a database
1010
beforeEach(cleanDatabase);

test/support/cleanDatabase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const container = require('src/container');
22
const database = container.resolve('database');
33

4-
module.exports = () => database.truncate({ cascade: true });
4+
module.exports = () => database && database.truncate({ cascade: true });

test/support/factories/.gitkeep

Whitespace-only changes.

yarn.lock

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ defaults@^1.0.0:
707707
dependencies:
708708
clone "^1.0.2"
709709

710-
del@^2.0.2:
710+
del@^2.0.2, del@^2.2.2:
711711
version "2.2.2"
712712
resolved "https://registry.yarnpkg.com/del/-/del-2.2.2.tgz#c12c981d067846c84bcaf862cff930d907ffd1a8"
713713
dependencies:
@@ -3221,6 +3221,14 @@ replace-ext@0.0.1:
32213221
version "0.0.1"
32223222
resolved "https://registry.yarnpkg.com/replace-ext/-/replace-ext-0.0.1.tgz#29bbd92078a739f0bcce2b4ee41e837953522924"
32233223

3224+
replace-in-file@^2.5.0:
3225+
version "2.5.0"
3226+
resolved "https://registry.yarnpkg.com/replace-in-file/-/replace-in-file-2.5.0.tgz#1f6388225c8acb4db288a8189365e266f9824da4"
3227+
dependencies:
3228+
chalk "^1.1.3"
3229+
glob "^7.1.1"
3230+
yargs "^7.0.1"
3231+
32243232
request@^2.79.0:
32253233
version "2.80.0"
32263234
resolved "https://registry.yarnpkg.com/request/-/request-2.80.0.tgz#8cc162d76d79381cdefdd3505d76b80b60589bd0"
@@ -4128,6 +4136,12 @@ yargs-parser@^4.2.0:
41284136
dependencies:
41294137
camelcase "^3.0.0"
41304138

4139+
yargs-parser@^5.0.0:
4140+
version "5.0.0"
4141+
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-5.0.0.tgz#275ecf0d7ffe05c77e64e7c86e4cd94bf0e1228a"
4142+
dependencies:
4143+
camelcase "^3.0.0"
4144+
41314145
yargs@^6.5.0:
41324146
version "6.6.0"
41334147
resolved "https://registry.yarnpkg.com/yargs/-/yargs-6.6.0.tgz#782ec21ef403345f830a808ca3d513af56065208"
@@ -4146,6 +4160,24 @@ yargs@^6.5.0:
41464160
y18n "^3.2.1"
41474161
yargs-parser "^4.2.0"
41484162

4163+
yargs@^7.0.1:
4164+
version "7.0.2"
4165+
resolved "https://registry.yarnpkg.com/yargs/-/yargs-7.0.2.tgz#115b97df1321823e8b8648e8968c782521221f67"
4166+
dependencies:
4167+
camelcase "^3.0.0"
4168+
cliui "^3.2.0"
4169+
decamelize "^1.1.1"
4170+
get-caller-file "^1.0.1"
4171+
os-locale "^1.4.0"
4172+
read-pkg-up "^1.0.1"
4173+
require-directory "^2.1.1"
4174+
require-main-filename "^1.0.1"
4175+
set-blocking "^2.0.0"
4176+
string-width "^1.0.2"
4177+
which-module "^1.0.0"
4178+
y18n "^3.2.1"
4179+
yargs-parser "^5.0.0"
4180+
41494181
yargs@~3.10.0:
41504182
version "3.10.0"
41514183
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"

0 commit comments

Comments
 (0)