1 | "use strict";
|
2 |
|
3 | module.exports = {
|
4 |
|
5 | |
6 |
|
7 |
|
8 |
|
9 | parse
|
10 | };
|
11 |
|
12 |
|
13 | const
|
14 | ChildProcess = require( "child_process" ),
|
15 | FS = require( "fs" ),
|
16 | Input = require( 'readline-sync' ),
|
17 | Path = require( "path" );
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function 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 |
|
40 |
|
41 |
|
42 | function 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 |
|
56 |
|
57 |
|
58 |
|
59 | function 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 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function 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 |
|
103 |
|
104 |
|
105 |
|
106 | function 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 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function 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 |
|
162 |
|
163 |
|
164 |
|
165 | function 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 |