Skip to content
This repository was archived by the owner on Apr 26, 2025. It is now read-only.

Commit 20efaff

Browse files
committed
cli: write tests for init command, add rimraf, add .npmignore
1 parent b419f5a commit 20efaff

File tree

7 files changed

+128
-29
lines changed

7 files changed

+128
-29
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__fixtures__
2+
__tests__

cli/__fixtures__/package/package.json

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

cli/__tests__/init-test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { spawnSync } = require('child_process')
2+
const path = require('path')
3+
const rimraf = require('rimraf')
4+
5+
const scriptPath = path.resolve(__dirname, '../index.js')
6+
const packagePath = path.resolve(__dirname, '../__fixtures__/package')
7+
const emptyPackagePath = path.resolve(__dirname, '../__fixtures__/emptyPackage')
8+
9+
require('./to-run-successfully-matcher')
10+
11+
describe('[CLI] init', () => {
12+
describe('react-tv init', () => {
13+
let slug = 'react-tv'
14+
15+
beforeEach((done) => rimraf(path.resolve(packagePath, slug), done))
16+
afterEach((done) => rimraf(path.resolve(packagePath, slug), done))
17+
18+
it('should create react-tv folder', () => {
19+
const subject = spawnSync('node', [
20+
scriptPath,
21+
'init'
22+
], { cwd: packagePath })
23+
24+
const createdAppPath = path.resolve(packagePath, slug)
25+
const appInfo = require(path.resolve(createdAppPath, 'webos/appinfo.json'))
26+
27+
expect(appInfo.id).toEqual('react.tv.app')
28+
expect(appInfo.title).toEqual('package')
29+
expect(subject).toRunSuccessfully()
30+
})
31+
32+
it('should fail when package.json not exists', () => {
33+
const subject = spawnSync('node', [
34+
scriptPath,
35+
'init'
36+
], { cwd: emptyPackagePath })
37+
38+
expect(subject).not.toRunSuccessfully()
39+
})
40+
})
41+
42+
describe('react-tv init <appName>', () => {
43+
const appName = 'russell-crowe'
44+
45+
beforeEach((done) => rimraf(path.resolve(emptyPackagePath, appName), done))
46+
afterEach((done) => rimraf(path.resolve(emptyPackagePath, appName), done))
47+
48+
it('should create custom app', () => {
49+
const subject = spawnSync('node', [
50+
scriptPath,
51+
'init',
52+
appName
53+
], { cwd: emptyPackagePath })
54+
55+
const createdAppPath = path.resolve(emptyPackagePath, appName)
56+
const createdPackageJson = require(path.resolve(createdAppPath, 'package.json'))
57+
58+
expect(subject).toRunSuccessfully()
59+
expect(createdPackageJson.name).toEqual('russell-crowe')
60+
})
61+
})
62+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
expect.extend({
2+
toRunSuccessfully(received) {
3+
const pass = received.status === 0
4+
5+
if (pass) {
6+
return {
7+
message: () => `expected exit code not to be 0`,
8+
pass: true,
9+
}
10+
} else {
11+
return {
12+
message: () => `expected exit code to be 0, was ${received.status} with error: \n${received.output}`,
13+
pass: false,
14+
}
15+
}
16+
}
17+
})

cli/shared/index.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,52 @@ function createReactTVApp(appName) {
3131

3232
if (appName) {
3333
appPath = `${appPath}/${appName}`;
34-
fs.copySync(customApp, path.resolve(appPath));
35-
replace({
36-
regex: '{{REACTTVAPP}}',
37-
replacement: appName,
38-
paths: [appName],
39-
recursive: true,
40-
silent: true,
41-
});
34+
try {
35+
fs.copySync(customApp, path.resolve(appPath));
36+
replace({
37+
regex: '{{REACTTVAPP}}',
38+
replacement: appName,
39+
paths: [appName],
40+
recursive: true,
41+
silent: true,
42+
});
43+
} catch(e) {
44+
return process.exit(1);
45+
}
4246

4347
debug('Done! 📺 ⭐');
44-
return;
48+
return process.exit(0);
4549
}
4650

4751
if (fs.existsSync(packageJson)) {
4852
existentProject = true;
4953
appName = require(packageJson).name;
5054
} else {
51-
return debug('package.json not founded');
55+
debug('package.json not founded');
56+
return process.exit(1);
5257
}
5358

5459
if (!appName) {
55-
return debug('package.json {name} property not exists');
60+
debug('package.json {name} property not exists');
61+
return process.exit(1);
5662
}
5763

5864
appPath = `${appPath}/react-tv`;
59-
fs.copySync(appTemplatePath, appPath);
60-
replace({
61-
regex: '{{REACTTVAPP}}',
62-
replacement: appName,
63-
paths: ['./react-tv'],
64-
recursive: true,
65-
silent: true,
66-
});
65+
try {
66+
fs.copySync(appTemplatePath, appPath);
67+
replace({
68+
regex: '{{REACTTVAPP}}',
69+
replacement: appName,
70+
paths: ['./react-tv'],
71+
recursive: true,
72+
silent: true,
73+
});
74+
} catch(e) {
75+
return process.exit(1);
76+
}
6777

6878
debug('Done! 📺 ⭐');
79+
process.exit(0);
6980
}
7081

7182
module.exports = {

package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
],
1515
"scripts": {
1616
"build": "node scripts/rollup/build.js",
17-
"build-all": "NODE_ENV=PROD node scripts/rollup/build.js",
18-
"prepublishOnly": "yarn test && yarn build-all",
19-
"prettier-stat": "node ./scripts/prettier/index",
20-
"test": "yarn prettier-stat && yarn flow && yarn jest",
21-
"jest": "jest",
22-
"jest-watch": "jest --watch",
17+
"build:all": "NODE_ENV=PROD node scripts/rollup/build.js",
18+
"prepublishOnly": "yarn test && yarn build:all",
19+
"prettier:stat": "node ./scripts/prettier/index",
20+
"test": "yarn prettier:stat && yarn flow && yarn jest:ci",
21+
"jest": "jest --testPathIgnorePatterns \"(__fixtures__|matcher.js)\"",
22+
"jest:ci": "jest --testPathIgnorePatterns \"(__fixtures__|matcher.js)\" --ci --runInBand",
2323
"flow": "flow",
2424
"prettier": "node ./scripts/prettier/index.js write-changed",
25-
"prettier-all": "node ./scripts/prettier/index.js write"
25+
"prettier:all": "node ./scripts/prettier/index.js write"
2626
},
2727
"dependencies": {
2828
"chalk": "^2.1.0",
@@ -44,6 +44,7 @@
4444
"react": "^16.0.0",
4545
"react-fiber-types": "file:src/renderer/types",
4646
"react-reconciler": "^0.6.0",
47+
"rimraf": "^2.6.2",
4748
"rollup": "^0.50.0",
4849
"rollup-plugin-babel": "^2.7.1",
4950
"rollup-plugin-commonjs": "^8.0.2",
@@ -82,7 +83,9 @@
8283
"webos"
8384
],
8485
"engines": {
85-
"node": ">=6"
86+
"node": ">=6",
87+
"npm": ">=3",
88+
"yarn": ">=0.22"
8689
},
8790
"author": "Raphael Amorim <rapha850@gmail.com>",
8891
"license": "MIT",
@@ -92,7 +95,8 @@
9295
"homepage": "https://github.com/raphamorim/react-tv#readme",
9396
"jest": {
9497
"roots": [
95-
"<rootDir>/src"
98+
"<rootDir>/src",
99+
"<rootDir>/cli"
96100
],
97101
"moduleDirectories": [
98102
"node_modules",

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2706,7 +2706,7 @@ right-align@^0.1.1:
27062706
dependencies:
27072707
align-text "^0.1.1"
27082708

2709-
rimraf@2, rimraf@^2.5.1, rimraf@^2.6.1:
2709+
rimraf@2, rimraf@^2.5.1, rimraf@^2.6.1, rimraf@^2.6.2:
27102710
version "2.6.2"
27112711
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
27122712
dependencies:

0 commit comments

Comments
 (0)