Skip to content

Commit a8cb1d2

Browse files
committed
init tutorial search logic, rely on node-file-exists
1 parent 998f1d1 commit a8cb1d2

File tree

18 files changed

+228
-99
lines changed

18 files changed

+228
-99
lines changed

lib/build/parser/import.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
var fs_1 = require('fs');
33
var path_1 = require('path');
4-
var file_1 = require('../../tools/file');
4+
var node_file_exists_1 = require('node-file-exists');
55
var cleanup_1 = require('./cleanup');
66
var settings_1 = require('./settings');
77
function loadImport(lines, pathToMd) {
@@ -10,7 +10,7 @@ function loadImport(lines, pathToMd) {
1010
pathToMd = pathToMd.concat('.md');
1111
}
1212
var realPath = path_1.join(process.cwd(), settings_1.tutorialDir, pathToMd);
13-
if (!file_1.fileExists(realPath)) {
13+
if (!node_file_exists_1.default(realPath)) {
1414
console.log('Invalid path to markdown file', realPath);
1515
return;
1616
}

lib/build/readme.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";
22
var fs_1 = require('fs');
3-
var file_1 = require('../tools/file');
3+
var node_file_exists_1 = require('node-file-exists');
44
var chalk_1 = require('chalk');
55
function createReadme() {
6-
if (!file_1.fileExists('./README.md')) {
6+
if (!node_file_exists_1.default('./README.md')) {
77
}
8-
if (!file_1.fileExists('./coderoad.json')) {
8+
if (!node_file_exists_1.default('./coderoad.json')) {
99
console.log(chalk_1.red('No coderoad.json file found'));
1010
return false;
1111
}
12-
if (!file_1.fileExists('./package.json')) {
12+
if (!node_file_exists_1.default('./package.json')) {
1313
console.log(chalk_1.red('No package.json file found'));
1414
return false;
1515
}

lib/cli.js

100755100644
File mode changed.

lib/create/write-demo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
var fs_1 = require('fs');
33
var path_1 = require('path');
44
var sort_package_json_1 = require('sort-package-json');
5-
var file_1 = require('../tools/file');
5+
var node_file_exists_1 = require('node-file-exists');
66
function createFile(pathToFile) {
7-
if (!file_1.fileExists(pathToFile)) {
7+
if (!node_file_exists_1.default(pathToFile)) {
88
var inputPath = path_1.join(__dirname, '..', '..', 'setup', pathToFile);
99
var test = fs_1.readFileSync(inputPath, 'utf8');
1010
fs_1.writeFileSync(pathToFile, test, 'utf8');
1111
}
1212
}
1313
function createFolder(pathToFolder) {
14-
if (!file_1.fileExists(pathToFolder)) {
14+
if (!node_file_exists_1.default(pathToFolder)) {
1515
fs_1.mkdirSync(pathToFolder);
1616
}
1717
}
@@ -40,7 +40,7 @@ function createTutorialMd() {
4040
exports.createTutorialMd = createTutorialMd;
4141
function createPackageJson(name) {
4242
return new Promise(function (resolve, reject) {
43-
if (!file_1.fileExists('package.json')) {
43+
if (!node_file_exists_1.default('package.json')) {
4444
var inputPath = path_1.join(__dirname, '..', '..', 'setup', 'package.json');
4545
var packageJson = JSON.parse(fs_1.readFileSync(inputPath, 'utf8'));
4646
packageJson.name = 'coderoad-' + name;

lib/tools/file.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/tutorials/find-tutorials.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var fs_1 = require('fs');
4+
var node_file_exists_1 = require('node-file-exists');
5+
var is_tutorial_1 = require('./is-tutorial');
6+
var update_1 = require('./update');
7+
function searchForTutorials(dir, deps) {
8+
if (!!deps && Object.keys(deps).length > 0) {
9+
return (Object.keys(deps)
10+
.filter(function (name) { return is_tutorial_1.isTutorial(dir, name); })
11+
.map(function (name) {
12+
var pathToTutorialPackageJson = path_1.join(dir, 'node_modules', name, 'package.json');
13+
if (!node_file_exists_1.default(pathToTutorialPackageJson)) {
14+
console.log("Error with " + name + ": no package.json file found. " + is_tutorial_1.tutorialError);
15+
return {
16+
name: name,
17+
version: 'NOT INSTALLED'
18+
};
19+
}
20+
var tutorialPackageJson = JSON.parse(fs_1.readFileSync(pathToTutorialPackageJson, 'utf8'));
21+
var version = tutorialPackageJson.version;
22+
return {
23+
name: name,
24+
version: version,
25+
latest: !!update_1.canUpdateTutorial(name, version)
26+
};
27+
}));
28+
}
29+
else {
30+
return [];
31+
}
32+
}
33+
exports.searchForTutorials = searchForTutorials;

lib/tutorials/is-tutorial.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var fs_1 = require('fs');
4+
var node_file_exists_1 = require('node-file-exists');
5+
exports.tutorialError = 'This is an error with the tutorial itself';
6+
function isTutorial(dir, name) {
7+
var pathToTutorialPackageJson = path_1.join(dir, 'node_modules', name, 'package.json');
8+
if (!node_file_exists_1.default(pathToTutorialPackageJson)) {
9+
console.log("Error with " + name + ": no package.json file found. " + exports.tutorialError);
10+
return false;
11+
}
12+
var packageJson = JSON.parse(fs_1.readFileSync(pathToTutorialPackageJson, 'utf8'));
13+
if (!packageJson.main && packageJson.main.match(/coderoad.json$/)) {
14+
console.log("Error with " + name + ": main does not load a coderoad.json file. " + exports.tutorialError);
15+
return false;
16+
}
17+
var pathToCoderoadJson = path_1.join(dir, 'node_modules', name, packageJson.main);
18+
if (!node_file_exists_1.default(pathToCoderoadJson)) {
19+
console.log("Error with " + name + ": no coderoad.json file. " + exports.tutorialError);
20+
return false;
21+
}
22+
;
23+
if (!packageJson.config || !packageJson.config.runner) {
24+
console.log("Error with " + name + ": no test runner specified. " + exports.tutorialError);
25+
return false;
26+
}
27+
return true;
28+
}
29+
exports.isTutorial = isTutorial;

lib/tutorials/update.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
var atom_plugin_command_line_1 = require('atom-plugin-command-line');
3+
function tutorialUpdate(name) {
4+
console.log("run \"npm install --save-dev " + name + "\"");
5+
}
6+
exports.tutorialUpdate = tutorialUpdate;
7+
function canUpdateTutorial(name, currentVersion) {
8+
if (!navigator.onLine) {
9+
return null;
10+
}
11+
return (atom_plugin_command_line_1.default('npm', "outdated " + name).then(function (res) {
12+
console.log(res);
13+
if (res.length > 0) {
14+
var linked = res.match(/[0-9\.]+\s+linked/);
15+
if (linked) {
16+
return false;
17+
}
18+
var match = res.match(/[0-9\.]+\s+[0-9\.]+\s+([0-9\.]+)/);
19+
if (match.length >= 2) {
20+
return true;
21+
}
22+
}
23+
return null;
24+
}));
25+
}
26+
exports.canUpdateTutorial = canUpdateTutorial;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
"*.md"
2525
],
2626
"dependencies": {
27+
"atom-plugin-command-line": "1.0.2",
2728
"chalk": "1.1.3",
2829
"commander": "2.9.0",
2930
"lodash.kebabcase": "4.0.1",
31+
"node-file-exists": "1.1.0",
3032
"prompt": "1.0.0",
3133
"sort-package-json": "^1.4.0",
3234
"validate-npm-package-name": "2.2.2"

src/build/parser/import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {readFileSync} from 'fs';
22
import {join} from 'path';
3-
import {fileExists} from '../../tools/file';
3+
import fileExists from 'node-file-exists';
44
import {trimQuotes} from './cleanup';
55
import {tutorialDir} from './settings';
66

src/build/readme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {readFileSync, writeFileSync} from 'fs';
2-
import {fileExists} from '../tools/file';
2+
import fileExists from 'node-file-exists';
33
import {red} from 'chalk';
44

55
export function createReadme(): boolean {

src/create/write-demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {readFileSync, writeFileSync, mkdirSync} from 'fs';
22
import {join} from 'path';
33
import {sortPackageJson} from 'sort-package-json'
4-
import {fileExists} from '../tools/file';
4+
import fileExists from 'node-file-exists';
55

66
function createFile(pathToFile: string): void {
77
if (!fileExists(pathToFile)) {

src/tools/file.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/tutorials/find-tutorials.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {join} from 'path';
2+
import {readFileSync} from 'fs';
3+
import fileExists from 'node-file-exists';
4+
import {isTutorial, tutorialError} from './is-tutorial';
5+
import {canUpdateTutorial} from './update';
6+
7+
export function searchForTutorials(dir: string, deps: Object): Tutorial.Info[] {
8+
if (!!deps && Object.keys(deps).length > 0) {
9+
return (Object.keys(deps)
10+
.filter((name: string) => isTutorial(dir, name))
11+
.map(function(name: string) {
12+
const pathToTutorialPackageJson = join(
13+
dir, 'node_modules', name, 'package.json'
14+
);
15+
// no package.json
16+
if (!fileExists(pathToTutorialPackageJson)) {
17+
console.log(
18+
`Error with ${name}: no package.json file found. ${tutorialError}`
19+
);
20+
return {
21+
name,
22+
version: 'NOT INSTALLED'
23+
};
24+
}
25+
26+
let tutorialPackageJson = JSON.parse(
27+
readFileSync(pathToTutorialPackageJson, 'utf8')
28+
);
29+
const version = tutorialPackageJson.version;
30+
31+
return {
32+
name,
33+
version,
34+
latest: !!canUpdateTutorial(name, version)
35+
};
36+
}));
37+
} else {
38+
return [];
39+
}
40+
}

src/tutorials/index.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,7 @@
1-
// import {join} from 'path';
21
import {yellow} from 'chalk';
3-
// import {fileExists} from '../tools/file';
4-
// import {readFileSync} from 'fs';
52

63
export default function tutorials(): void {
74
console.log(`List of tutorial packages in this directory...\n`);
85
console.log(yellow('This feature is not yet implemented'));
96
// getTutorials();
107
}
11-
12-
// function loadRootPackageJson(): PackageJson {
13-
// let pathToPackageJson = join(process.cwd(), './package.json');
14-
// if (fileExists(pathToPackageJson)) {
15-
// return JSON.parse(readFileSync(pathToPackageJson, 'utf8'));
16-
// } else {
17-
// console.log('no package.json file available. Try typing "npm init" in terminal');
18-
// process.exit(1);
19-
// }
20-
// }
21-
22-
// function isTutorial(name: string): boolean {
23-
// let pathToTutorialPackageJson = join(process.cwd(), 'node_modules', name, 'package.json');
24-
// if (fileExists(pathToTutorialPackageJson)) {
25-
// // has package.json
26-
// let packageJson = JSON.parse(readFileSync(pathToTutorialPackageJson, 'utf8'));
27-
// // main path to coderoad.json
28-
// if (packageJson.main && packageJson.main.match(/coderoad.json$/)) {
29-
// let pathToCoderoadJson = join(process.cwd(), 'node_modules', name, packageJson.main);
30-
// // coderoad.json file exists
31-
// if (fileExists(pathToCoderoadJson)) {
32-
// return true;
33-
// }
34-
// }
35-
// }
36-
// return false;
37-
// }
38-
//
39-
// function searchForTutorials(location): string[] {
40-
// if (!!location) {
41-
// return Object.keys(location)
42-
// .filter((name) => isTutorial(name));
43-
// } else {
44-
// return [];
45-
// }
46-
// }
47-
48-
// function getTutorials(): void {
49-
// let packageJson: PackageJson = loadRootPackageJson();
50-
// let tutorials: string[] = []
51-
// .concat(searchForTutorials(packageJson.dependencies))
52-
// .concat(searchForTutorials(packageJson.devDependencies));
53-
// if (tutorials.length) {
54-
// console.log('Available tutorials: ');
55-
// tutorials.forEach((tutorial: string) => {
56-
// console.log(' - ' + tutorial);
57-
// });
58-
// }
59-
// console.log('No tutorials available');
60-
// }

src/tutorials/is-tutorial.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {join} from 'path';
2+
import {readFileSync} from 'fs';
3+
import fileExists from 'node-file-exists';
4+
5+
export const tutorialError = 'This is an error with the tutorial itself';
6+
7+
export function isTutorial(dir: string, name: string): boolean {
8+
// has package.json
9+
const pathToTutorialPackageJson = join(
10+
dir, 'node_modules', name, 'package.json'
11+
);
12+
if (!fileExists(pathToTutorialPackageJson)) {
13+
console.log(`Error with ${name}: no package.json file found. ${tutorialError}`);
14+
return false;
15+
}
16+
// main path to coderoad.json
17+
let packageJson = JSON.parse(readFileSync(pathToTutorialPackageJson, 'utf8'));
18+
if (!packageJson.main && packageJson.main.match(/coderoad.json$/)) {
19+
console.log(`Error with ${name}: main does not load a coderoad.json file. ${tutorialError}`);
20+
return false;
21+
}
22+
// coderoad.json file exists
23+
let pathToCoderoadJson = join(
24+
dir, 'node_modules', name, packageJson.main
25+
);
26+
if (!fileExists(pathToCoderoadJson)) {
27+
console.log(`Error with ${name}: no coderoad.json file. ${tutorialError}`);
28+
return false;
29+
};
30+
if (!packageJson.config || !packageJson.config.runner) {
31+
console.log(`Error with ${name}: no test runner specified. ${tutorialError}`);
32+
return false;
33+
}
34+
35+
// let currentTutorialVersion: string = packageJson.dependencies[name] || packageJson.devDependencies[name];
36+
// canUpdateTutorial(name, currentTutorialVersion);
37+
38+
// let pathToTestRunner = path.join(dir, 'node_modules', packageJson.config.testRunner);
39+
// // if (!fileExists(pathToTestRunner)) {
40+
// // console.log(`Error with ${name}: ${packageJson.config.testRunner} test runner not installed`);
41+
// // return false;
42+
// // }
43+
return true;
44+
}

0 commit comments

Comments
 (0)