UNPKG

5.13 kBJavaScriptView Raw
1"use strict";
2
3module.exports = {
4
5 /**
6 * @param {object} project - Instance of a Project.
7 * @returns {object} Parsing of the configuration.
8 */
9 parse
10};
11
12
13const
14 ChildProcess = require( "child_process" ),
15 FS = require( "fs" ),
16 Input = require( 'readline-sync' ),
17 Path = require( "path" );
18
19
20/**
21 * @param {sring} projectDir - Root path of the Project.
22 * @returns {object} Parsing of the configuration.
23 */
24function parse( projectDir ) {
25 const
26 configFilename = Path.join( projectDir, "package.json" ),
27 config = load( configFilename );
28
29 enrichConfigWithGitParams( config );
30 addMissingConfigAttributes( config, projectDir );
31 checkCompilationType( config );
32 overwriteIfDifferent( config, configFilename );
33
34 return config;
35}
36
37
38/**
39 * @param {string} configFilename - Full path of the JSOn config file.
40 * @returns {object} Parsing of a JSON config.
41 */
42function load( configFilename ) {
43 if ( !FS.existsSync( configFilename ) ) {
44 throw Error( `Unable to find configuration file: ${configFilename}!` );
45 }
46 try {
47 const content = FS.readFileSync( configFilename );
48 return JSON.parse( content );
49 } catch ( ex ) {
50 throw Error( `Invalid JSON configuration file: ${configFilename}!\n${ex}` );
51 }
52}
53
54/**
55 * @param {object} config - Current configuration.
56 * @param {string} configFilename - Full path of the configuration file.
57 * @returns {boolean} `true` if the file hsa been overwritten.
58 */
59function overwriteIfDifferent( config, configFilename ) {
60 const
61 oldConfigAsString = FS.readFileSync( configFilename ),
62 newConfigAsString = JSON.stringify( config, null, ' ' );
63 if ( oldConfigAsString !== newConfigAsString ) {
64 return false;
65 }
66 FS.writeFileSync( configFilename, newConfigAsString );
67 return true;
68}
69/**
70 * Check for attribute `repository` in config. if not exists, retrieve it from command
71 * `git remote -v`, greping on `origin` then `(fetch)`.
72 * @param {object} config - Config object.
73 * @returns {undefined}
74 */
75function enrichConfigWithGitParams( config ) {
76 const
77 ORIGIN = "origin",
78 ORIGIN_LENGTH = ORIGIN.length,
79 FETCH = " (fetch)",
80 FETCH_LENGTH = FETCH.length;
81
82 if ( !config.repository || !config.repository.url ) {
83 const remotes = ChildProcess.execSync( "git remote -v" ).toString();
84 let origin = '';
85 remotes.split( "\n" ).forEach( function forEachRemote( remote ) {
86 if ( remote.substr( 0, ORIGIN_LENGTH ) === ORIGIN ) {
87 const restOfLine = remote.substr( ORIGIN_LENGTH );
88 if ( restOfLine.substr( -FETCH_LENGTH ) === FETCH ) {
89 origin = restOfLine.substr( 0, restOfLine.length - FETCH_LENGTH ).trim();
90 }
91 }
92 } );
93 config.repository = {
94 type: "git",
95 url: origin
96 };
97 }
98}
99
100
101/**
102 * @param {object} config - Config object.
103 * @param {string} projectPath - Root path of the Project.
104 * @returns {undefined}
105 */
106function addMissingConfigAttributes( config, projectPath ) {
107 const githubUrl = getGithubUrl( config );
108
109 config.homepage = config.homepage || githubUrl;
110 config.bugs = config.bugs || { url: `${githubUrl}/issues` };
111 config.scripts = config.scripts || {
112 test: "jasmine",
113 "test:dbg": "node --debug-brk node_modules/jasmine/bin/jasmine.js"
114 };
115 if ( !config.name ) {
116 const
117 projectName = Path.basename( projectPath ),
118 answer = Input.question( `Project's name [${projectName}]: ` );
119
120 if ( answer.trim().length > 0 ) {
121 config.name = answer;
122 } else {
123 config.name = projectName;
124 }
125 }
126 config.version = config.version || "0.0.1";
127 if ( !config.author ) {
128 const answer = Input.question( "Author: " );
129 config.author = answer.trim();
130 }
131 if ( !config.description ) {
132 const answer = Input.question( "Description: " );
133 config.description = answer.trim();
134 }
135 config.license = config.license || "GPL-3.0";
136 config.tfw = config.tfw || {
137 modules: [],
138 compile: {
139 type: "web",
140 files: "\\.html$"
141 }
142 };
143}
144
145/**
146 * Prior to version 0.46, compilation types were `firefoxos` and `nodewebkit`.
147 * Now, we use `web` and `desktop`.
148 * @param {object} config - Configuration as object.
149 * @returns {undefined}
150 */
151function checkCompilationType( config ) {
152 if ( config.tfw.compile.type === 'firefoxos' ) {
153 config.tfw.compile.type = 'web';
154 } else {
155 config.tfw.compile.type = 'desktop';
156 }
157}
158
159
160/**
161 * @param {object} config - Configuration.
162 * @param {string} config.repository.url - URL of the git repository on Github.
163 * @return {string} The URL without the `.git` extension.
164 */
165function getGithubUrl( config ) {
166 const
167 GIT_EXTENSION = ".git",
168 GIT_EXTENSION_LENGTH = GIT_EXTENSION.length,
169 fullUrl = config.repository.url;
170
171 return fullUrl.substr( 0, fullUrl.length - GIT_EXTENSION_LENGTH );
172}
\No newline at end of file