Github user stevengill commented on a diff in the pull request:
https://github.com/apache/cordova-lib/pull/456#discussion_r67077184
--- Diff: cordova-lib/src/cordova/create.js ---
@@ -252,133 +249,116 @@ function create(dir, optionalId, optionalName, cfg,
fetchOpt) {
www: import_from_path
};
- // Keep going into child "www" folder if exists in stock app
package.
+ // Keep going into child "www" folder if exists in stock app
package; used to find config.xml
while (fs.existsSync(path.join(paths.www, 'www'))) {
paths.root = paths.www;
paths.www = path.join(paths.root, 'www');
}
- // find config.xml
+ // find config.xml OR get stock config.xml, used if template does
not contain config.xml
if (fs.existsSync(path.join(paths.root, 'config.xml'))) {
paths.configXml = path.join(paths.root, 'config.xml');
- paths.configXmlLinkable = true;
} else {
- try {
- paths.configXml =
- path.join(require('cordova-app-hello-world').dirname,
- 'config.xml');
- } catch (e) {
- // Falling back on npm@2 path hierarchy
- // TODO: Remove fallback after cordova-app-hello-world
release
- paths.configXml =
- path.join(__dirname, '..', '..', 'node_modules',
- 'cordova-app-hello-world', 'config.xml');
- }
- }
- if (fs.existsSync(path.join(paths.root, 'merges'))) {
- paths.merges = path.join(paths.root, 'merges');
- } else {
- // No merges by default
- }
- if (fs.existsSync(path.join(paths.root, 'hooks'))) {
- paths.hooks = path.join(paths.root, 'hooks');
- paths.hooksLinkable = true;
- } else {
- try {
- paths.hooks =
- path.join(require('cordova-app-hello-world').dirname,
- 'hooks');
- } catch (e) {
- // Falling back on npm@2 path hierarchy
- // TODO: Remove fallback after cordova-app-hello-world
release
- paths.hooks =
- path.join(__dirname, '..', '..', 'node_modules',
- 'cordova-app-hello-world', 'hooks');
- }
+ paths.configXml =
path.join(require('cordova-app-hello-world').dirname, 'config.xml');
}
+ // get stock www; used if template does not contain www
+ paths.www = path.join(require('cordova-app-hello-world').dirname,
'www');
+
+ // get stock hooks; used if template does not contain hooks
+ paths.hooks =
path.join(require('cordova-app-hello-world').dirname, 'hooks');
+
+
var dirAlreadyExisted = fs.existsSync(dir);
if (!dirAlreadyExisted) {
fs.mkdirSync(dir);
}
-
- var tryToLink = !!cfg.lib.www.link;
- function copyOrLink(src, dst, linkable) {
- if (src) {
- if (tryToLink && linkable) {
- fs.symlinkSync(src, dst, 'dir');
- } else {
- shell.mkdir(dst);
- shell.cp('-R', path.join(src, '*'), dst);
- }
- }
- }
-
- /*
- Copies template files, and directories into a Cordova project
directory.
- Files, and directories not copied include: www, mergers,platforms,
- plugins, hooks, and config.xml. A template directory, and platform
- directory must be passed.
-
- templateDir - Template directory
- projectDir - Project directory
- */
- function copyTemplateFiles(templateDir, projectDir) {
- var templateFiles; // Current file
-
- templateFiles = fs.readdirSync(templateDir);
-
- // Remove directories, and files that are automatically copied
- templateFiles = templateFiles.filter(
- function (value) {
- return !(value === 'www' || value === 'mergers' ||
- value === 'config.xml' || value === 'hooks');
- }
- );
-
- // Copy each template file
- for (var i = 0; i < templateFiles.length; i++)
- shell.cp('-R', path.resolve(templateDir,
templateFiles[i]), projectDir);
- }
-
try {
- copyOrLink(paths.www, path.join(dir, 'www'), true);
- copyOrLink(paths.merges, path.join(dir, 'merges'), true);
- copyOrLink(paths.hooks, path.join(dir, 'hooks'),
- paths.hooksLinkable);
-
+ // Copy files from template to project
if (cfg.lib.www.template)
- copyTemplateFiles(import_from_path, dir);
+ copyTemplateFiles(import_from_path, dir, isSubDir);
- if (paths.configXml) {
- if (tryToLink && paths.configXmlLinkable) {
- fs.symlinkSync(paths.configXml, path.join(dir,
'config.xml'));
- } else {
- shell.cp(paths.configXml, path.join(dir,
'config.xml'));
- }
+ // If following were not copied from template, copy from stock
app hello world
+ ifNotCopied(paths.www, path.join(dir, 'www'));
+ ifNotCopied(paths.hooks, path.join(dir, 'hooks'));
+ if (!fs.existsSync(path.join(dir, 'config.xml')) &&
paths.configXml) {
+ shell.cp(paths.configXml, path.join(dir, 'config.xml'));
}
} catch (e) {
if (!dirAlreadyExisted) {
shell.rm('-rf', dir);
}
- if (process.platform.slice(0, 3) == 'win' && e.code ==
'EPERM') {
- throw new CordovaError('Symlinks on Windows require
Administrator privileges');
- }
throw e;
}
+
+ // Update package.json name and version fields.
+ if (fs.existsSync(path.join(dir, 'package.json'))) {
+ var pkgjson = require(path.resolve(dir, 'package.json'));
+ if (cfg.name) {
+ pkgjson.name = cfg.name.toLowerCase();
+ }
+ pkgjson.version = '1.0.0';
+ fs.writeFile(path.join(dir, 'package.json'),
JSON.stringify(pkgjson), function (err) {
+ if (err) throw new CordovaError('Could not write
package.json');
+ events.emit('verbose', 'writing name to package.json');
+ });
+ }
+
// Create basic project structure.
if (!fs.existsSync(path.join(dir, 'platforms')))
shell.mkdir(path.join(dir, 'platforms'));
if (!fs.existsSync(path.join(dir, 'plugins')))
shell.mkdir(path.join(dir, 'plugins'));
- // Write out id and name to config.xml
+ // Write out id and name to config.xml; set version to 1.0.0 (to
match package.json default version)
var configPath = cordova_util.projectConfig(dir);
var conf = new ConfigParser(configPath);
if (cfg.id) conf.setPackageName(cfg.id);
if (cfg.name) conf.setName(cfg.name);
+ conf.setVersion('1.0.0');
conf.write();
});
}
+
+/**
+ * Recursively copies folder to destination if folder is not found in
destination.
+ * @param {string} src for copying
+ * @param {string} dst for copying
+ * @return No return value
+ */
+function ifNotCopied(src, dst) {
+ if (!fs.existsSync(dst) && src) {
+ shell.mkdir(dst);
+ shell.cp('-R', path.join(src, '*'), dst);
+ }
+}
+
+/**
+ * Copies template files, and directories into a Cordova project directory.
+ * If the template exists in a subdirectory everything is copied.
+ * Otherwise package.json, RELEASENOTES.md, .git, NOTICE, LICENSE,
COPYRIGHT, and .npmignore are not copied over.
+ * A template directory, and platform directory must be passed.
--- End diff --
I think you meant project directory not platform directory in this comment
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]