Skip to content

Commit a081f3c

Browse files
committed
load imports from package.json config dir path
1 parent 787ab4b commit a081f3c

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

lib/build/parser/import.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ var fs_1 = require('fs');
33
var path_1 = require('path');
44
var file_1 = require('../../tools/file');
55
var cleanup_1 = require('./cleanup');
6+
var settings_1 = require('./settings');
67
function loadImport(lines, pathToMd) {
78
pathToMd = cleanup_1.trimQuotes(pathToMd);
89
if (!pathToMd.match(/\.md$/)) {
910
pathToMd = pathToMd.concat('.md');
1011
}
11-
var realPath = path_1.join(process.cwd(), pathToMd);
12-
if (!file_1.fileExists(pathToMd)) {
12+
var realPath = path_1.join(process.cwd(), settings_1.tutorialDir, pathToMd);
13+
if (!file_1.fileExists(realPath)) {
1314
console.log('Invalid path to markdown file', realPath);
1415
return;
1516
}

lib/build/parser/settings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
var fs_1 = require('fs');
3+
var path_1 = require('path');
4+
var chalk_1 = require('chalk');
5+
var settings = null;
6+
try {
7+
settings = JSON.parse(fs_1.readFileSync(path_1.join(process.cwd(), 'package.json'), 'utf8'));
8+
}
9+
catch (e) {
10+
console.log(chalk_1.red('No package.json config found in directory ', process.cwd(), e));
11+
}
12+
exports.tutorialDir = settings.dir || 'tutorial';

lib/build/parser/tutorialDir.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
exports.tutorialDir = 'tutorial';

src/build/parser/import.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import {readFileSync} from 'fs';
22
import {join} from 'path';
33
import {fileExists} from '../../tools/file';
44
import {trimQuotes} from './cleanup';
5+
import {tutorialDir} from './settings';
56

67
export function loadImport(lines: string[], pathToMd: string): string[] {
8+
// add .md suffix
79
pathToMd = trimQuotes(pathToMd);
810
if (!pathToMd.match(/\.md$/)) {
911
pathToMd = pathToMd.concat('.md');
1012
}
11-
let realPath: string = join(process.cwd(), pathToMd);
12-
if (!fileExists(pathToMd)) {
13+
// get path to imports
14+
let realPath: string = join(process.cwd(), tutorialDir, pathToMd);
15+
if (!fileExists(realPath)) {
1316
console.log('Invalid path to markdown file', realPath);
1417
return;
1518
}

src/build/parser/settings.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {readFileSync} from 'fs';
2+
import {join} from 'path';
3+
import {red} from 'chalk';
4+
5+
let settings = null;
6+
try {
7+
settings = JSON.parse(
8+
readFileSync(
9+
join(process.cwd(), 'package.json'), 'utf8'
10+
)
11+
);
12+
} catch (e) {
13+
console.log(red('No package.json config found in directory ', process.cwd(), e));
14+
}
15+
16+
export const tutorialDir = settings.dir || 'tutorial';

src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ program
1313
.version('0.3.27')
1414
.usage('[options] <keywords>')
1515
.option('-b, --build [path/to/tutorial.md]',
16-
'tutorial markdown file', /^.+\.md$/i)
16+
'tutorial markdown file', /^.+\.md$/i)
1717
.option('-c, --create [name]', 'tutorial name')
1818
.option('-p, --publish [version]',
19-
'publish tutorial to npm with new version number')
19+
'publish tutorial to npm with new version number')
2020
.option('-t, --tutorials', 'list of tutorial packages')
2121
.option('-s, --search [query]', 'search for tutorial package')
2222
.option('-r, --run', 'run tutorial')
@@ -55,6 +55,6 @@ switch (true) {
5555
program.help();
5656
}
5757

58-
// exit
58+
// success! exit
5959
process.stdout.write(green(' ✓\n'));
6060
process.exit(0);

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"src/build/parser/info.ts",
2424
"src/build/parser/match.ts",
2525
"src/build/parser/page.ts",
26+
"src/build/parser/settings.ts",
2627
"src/build/parser/task.ts",
2728
"src/build/readme.ts",
2829
"src/build/validators.ts",

0 commit comments

Comments
 (0)