|
1 | 1 | "use strict";
|
| 2 | +var process = require('process'); |
| 3 | +var path = require('path'); |
2 | 4 | var chalk = require('chalk');
|
| 5 | +var fs = require('fs'); |
3 | 6 | function tutorials() {
|
4 |
| - console.log("List of tutorial packages..."); |
5 |
| - console.log(chalk.yellow('"Tutorials" feature not implemented yet.')); |
| 7 | + console.log("List of tutorial packages in this directory...\n"); |
| 8 | + console.log(chalk.yellow('This feature is not yet implemented')); |
6 | 9 | }
|
7 | 10 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | 11 | exports.default = tutorials;
|
| 12 | +function fileExists(path) { |
| 13 | + try { |
| 14 | + fs.accessSync(path, fs.R_OK | fs.W_OK); |
| 15 | + } |
| 16 | + catch (e) { |
| 17 | + if (e) { |
| 18 | + console.log(e); |
| 19 | + return false; |
| 20 | + } |
| 21 | + } |
| 22 | + return true; |
| 23 | +} |
| 24 | +function loadRootPackageJson() { |
| 25 | + var pathToPackageJson = path.join(process.cwd(), './package.json'); |
| 26 | + if (fileExists(pathToPackageJson)) { |
| 27 | + return JSON.parse(fs.readFileSync(pathToPackageJson, 'utf8')); |
| 28 | + } |
| 29 | + else { |
| 30 | + console.log('no package.json file available. Try typing "npm init" in terminal'); |
| 31 | + process.exit(1); |
| 32 | + } |
| 33 | +} |
| 34 | +function isTutorial(name) { |
| 35 | + var pathToTutorialPackageJson = path.join(process.cwd(), 'node_modules', name, 'package.json'); |
| 36 | + if (fileExists(pathToTutorialPackageJson)) { |
| 37 | + var packageJson = JSON.parse(fs.readFileSync(pathToTutorialPackageJson, 'utf8')); |
| 38 | + if (packageJson.main && packageJson.main.match(/coderoad.json$/)) { |
| 39 | + var pathToCoderoadJson = path.join(process.cwd(), 'node_modules', name, packageJson.main); |
| 40 | + if (fileExists(pathToCoderoadJson)) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + return false; |
| 46 | +} |
| 47 | +function searchForTutorials(location) { |
| 48 | + if (!!location) { |
| 49 | + return Object.keys(location) |
| 50 | + .filter(function (name) { return isTutorial(name); }); |
| 51 | + } |
| 52 | + else { |
| 53 | + return []; |
| 54 | + } |
| 55 | +} |
| 56 | +function getTutorials() { |
| 57 | + var packageJson = loadRootPackageJson(); |
| 58 | + var tutorials = [] |
| 59 | + .concat(searchForTutorials(packageJson.dependencies)) |
| 60 | + .concat(searchForTutorials(packageJson.devDependencies)); |
| 61 | + if (tutorials.length) { |
| 62 | + console.log('Available tutorials: '); |
| 63 | + tutorials.forEach(function (tutorial) { |
| 64 | + console.log(' - ' + tutorial); |
| 65 | + }); |
| 66 | + } |
| 67 | + console.log('No tutorials available'); |
| 68 | +} |
0 commit comments