/
init-defaults.js
141 lines (122 loc) · 4.5 KB
/
init-defaults.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
**/
/* global dirname */
/* global basename */
/* global yes */
/* global prompt */
// PromZard file that is used by createpackagejson and init-package-json module
// The PromZard context is also provided via this. Make use of this to avoid
// having to use the reserved word package. This way we can lint this file.
const pkg = this.package;
var fs = require('fs-extra');
var path = require('path');
var defaults = require('./defaults.json');
function readDeps (test) {
return function (cb) {
fs.readdir('node_modules', function (er, dir) {
if (er) return cb();
var deps = {};
var n = dir.length;
if (n === 0) return cb(null, deps);
dir.forEach(function (d) {
if (d.match(/^\./)) return next();
var dp = path.join(dirname, 'node_modules', d, 'package.json');
fs.readFile(dp, 'utf8', function (er, p) {
if (er) return next();
try { p = JSON.parse(p); } catch (e) { return next(); }
if (!p.version) return next();
deps[d] = undefined + p.version;
return next();
});
});
function next () {
if (--n === 0) return cb(null, deps);
}
});
};
}
var name = pkg.name || defaults.id || basename;
exports.name = yes ? name : prompt('name', name);
var version = pkg.version || defaults.version || '1.0.0';
exports.version = yes ? version : prompt('version', version);
if (!pkg.description) {
if (defaults.description) {
exports.description = defaults.description;
} else {
exports.description = yes ? '' : prompt('description');
}
}
if (!pkg.cordova) {
exports.cordova = {};
if (defaults.id) {
exports.cordova.id = defaults.id;
}
if (defaults.platforms) {
exports.cordova.platforms = defaults.platforms;
}
}
if (!pkg.dependencies) {
exports.dependencies = readDeps(false);
}
if (!pkg.devDependencies) {
exports.devDependencies = readDeps(true);
}
if (!pkg.repository) {
exports.repository = function (cb) {
fs.readFile('.git/config', 'utf8', function (er, gconf) {
if (er || !gconf) {
if (defaults.repository) {
return cb(null, yes ? defaults.repository : prompt('git repository', defaults.repository));
}
return cb(null, yes ? '' : prompt('git repository'));
}
gconf = gconf.split(/\r?\n/);
var i = gconf.indexOf('[remote "origin"]');
var u;
if (i !== -1) {
u = gconf[i + 1];
if (!u.match(/^\s*url =/)) u = gconf[i + 2];
if (!u.match(/^\s*url =/)) u = null;
else u = u.replace(/^\s*url = /, '');
}
if (u && u.match(/^git@github.com:/)) { u = u.replace(/^git@github.com:/, 'https://github.com/'); }
return cb(null, yes ? u : prompt('git repository', u));
});
};
}
if (!pkg.keywords) {
if (defaults.keywords) {
exports.keywords = defaults.keywords;
} else {
exports.keywords = yes ? '' : prompt('keywords', function (s) {
if (!s) return undefined;
if (Array.isArray(s)) s = s.join(' ');
if (typeof s !== 'string') return s;
return s.split(/[\s,]+/);
});
}
}
if (!pkg.engines) {
if (defaults.engines && defaults.engines.length > 0) {
exports.engines = defaults.engines;
}
}
if (!pkg.author) {
exports.author = prompt('author');
}
const license = pkg.license || defaults.license || 'ISC';
exports.license = yes ? license : prompt('license', license);