UNPKG

1.99 kBJavaScriptView Raw
1"use strict";
2
3var FS = require("fs");
4var Path = require("path");
5
6
7/**
8 * This is the context of a project. A project is definied by its `package.json` file.
9 */
10var Context = function( packageFilename, options ) {
11 if( typeof options === 'undefined' ) options = {};
12
13 packageFilename = Path.resolve( packageFilename );
14 if( false == FS.existsSync() )
15 this.fatal( "Package file not found: \"" + packageFilename + "\"" );
16
17 var prjDir = Path.dirname( packageFilename );
18 this._prjDir = Path.resolve( prjDir );
19 this._libDir = Path.resolve( Path.join(__dirname, "../ker") );
20 this._tplDir = Path.resolve( Path.join(__dirname, "../tpl") );
21 this._srcDir = mkdir.call( this, prjDir, "src" );
22 this._docDir = mkdir.call( this, prjDir, "doc" );
23 this._tmpDir = mkdir.call( this, prjDir, "tmp" );
24 this._wwwDir = mkdir.call( this, prjDir, "www" );
25
26};
27
28
29/**
30 * @member Context.fatal
31 * @param
32 */
33Context.prototype.fatal = function( msg ) {
34 throw msg;
35};
36
37
38/**
39 * @param arguments all arguments will be joined to form the path of the directory to create.
40 * @return the name of the created directory.
41 */
42function mkdir( dir ) {
43 var key, arg, items = [];
44 for (key in arguments) {
45 arg = arguments[key].trim();
46 items.push(arg);
47 }
48 var path = Path.resolve(Path.normalize(items.join("/"))),
49 item, i,
50 curPath = "";
51 items = path.replace(/\\/g, '/').split("/");
52 for (i = 0 ; i < items.length ; i++) {
53 item = items[i];
54 curPath += item + "/";
55 if (FS.existsSync(curPath)) {
56 var stat = FS.statSync(curPath);
57 if (!stat.isDirectory()) {
58 break;
59 }
60 } else {
61 try {
62 FS.mkdirSync(curPath);
63 }
64 catch (ex) {
65 this.fatal( "Unable to create directory \"" + curPath + "\"!\n" + ex );
66 }
67 }
68 }
69 return path;
70}
71
72
73module.exports = Context;