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

0 commit comments

Comments
 (0)