UNPKG

6.48 kBJavaScriptView Raw
1/*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20var Q = require('q');
21var fs = require('fs');
22var path = require('path');
23var shell = require('shelljs');
24var uuid = require('node-uuid');
25var events = require('cordova-common').events;
26var CordovaError = require('cordova-common').CordovaError;
27var AppxManifest = require('../../template/cordova/lib/AppxManifest');
28var pkg = require('../../package');
29
30// Creates cordova-windows project at specified path with specified namespace, app name and GUID
31module.exports.create = function (destinationDir, config, options) {
32 if (!destinationDir) return Q.reject('No destination directory specified.');
33
34 var projectPath = path.resolve(destinationDir);
35 if (fs.existsSync(projectPath)) {
36 return Q.reject(new CordovaError('Project directory already exists:\n\t' + projectPath));
37 }
38
39 // Set parameters/defaults for create
40 var packageName = (config && config.packageName()) || 'Cordova.Example';
41 var appName = (config && config.name()) || 'CordovaAppProj';
42 // 64 symbols restriction goes from manifest schema definition
43 // http://msdn.microsoft.com/en-us/library/windows/apps/br211415.aspx
44 var safeAppName = appName.length <= 64 ? appName : appName.substr(0, 64);
45 var templateOverrides = options.customTemplate;
46 var guid = options.guid || uuid.v1();
47 var root = path.join(__dirname, '..', '..');
48
49 events.emit('log', 'Creating Cordova Windows Project:');
50 events.emit('log', '\tPath: ' + path.relative(process.cwd(), projectPath));
51 events.emit('log', '\tNamespace: ' + packageName);
52 events.emit('log', '\tName: ' + appName);
53 if (templateOverrides) {
54 events.emit('log', '\tCustomTemplatePath: ' + templateOverrides);
55 }
56
57 // Copy the template source files to the new destination
58 events.emit('verbose', 'Copying windows template project to ' + projectPath);
59 shell.cp('-rf', path.join(root, 'template', '*'), projectPath);
60
61 // Duplicate cordova.js to platform_www otherwise it will get removed by prepare
62 shell.cp('-rf', path.join(root, 'template/www/cordova.js'), path.join(projectPath, 'platform_www'));
63 // Duplicate splashscreen.css to platform_www otherwise it will get removed by prepare
64 var cssDirectory = path.join(projectPath, 'platform_www', 'css');
65 recursiveCreateDirectory(cssDirectory);
66 shell.cp('-rf', path.join(root, 'template/www/css/splashscreen.css'), cssDirectory);
67
68 // Copy cordova-js-src directory
69 events.emit('verbose', 'Copying cordova-js sources to platform_www');
70 shell.cp('-rf', path.join(root, 'cordova-js-src'), path.join(projectPath, 'platform_www'));
71
72 // Copy our unique VERSION file, so peeps can tell what version this project was created from.
73 shell.cp('-rf', path.join(root, 'VERSION'), projectPath);
74
75 // copy node_modules to cordova directory
76 let nodeModulesDir = path.join(root, 'node_modules');
77 if (fs.existsSync(nodeModulesDir)) {
78 events.emit('verbose', 'Copying node_modules to ' + projectPath);
79 shell.cp('-r', nodeModulesDir, path.join(projectPath, 'cordova'));
80 }
81
82 // copy check_reqs module to cordova directory
83 shell.cp('-rf', path.join(root, 'bin', 'check_reqs*'), path.join(projectPath, 'cordova'));
84 shell.cp('-rf', path.join(root, 'bin', 'lib', 'check_reqs*'), path.join(projectPath, 'cordova', 'lib'));
85
86 if (templateOverrides && fs.existsSync(templateOverrides)) {
87 events.emit('verbose', 'Copying windows template overrides from ' + templateOverrides + ' to ' + projectPath);
88 shell.cp('-rf', templateOverrides, projectPath);
89 }
90
91 // Copy base.js into the target project directory
92 var destinationDirectory = path.join(projectPath, 'platform_www', 'WinJS', 'js');
93 var destBaseJsPath = path.join(destinationDirectory, 'base.js');
94 var srcBaseJsPath = require.resolve('winjs/js/base');
95 recursiveCreateDirectory(destinationDirectory);
96 shell.cp('-f', srcBaseJsPath, destBaseJsPath);
97
98 // CB-12042 Also copy base.js to www directory
99 shell.mkdir('-p', path.join(projectPath, 'www/WinJS/js'));
100 shell.cp('-f', srcBaseJsPath, path.join(projectPath, 'www/WinJS/js/base.js'));
101
102 // replace specific values in manifests' templates
103 events.emit('verbose', 'Updating manifest files with project configuration.');
104 [ 'package.windows.appxmanifest', 'package.phone.appxmanifest',
105 'package.windows10.appxmanifest' ]
106 .forEach(function (item) {
107 var manifest = AppxManifest.get(path.join(projectPath, item));
108 if (manifest.hasPhoneIdentity) {
109 manifest.getPhoneIdentity().setPhoneProductId(guid);
110 }
111
112 manifest.setPackageName(packageName)
113 .setAppName(safeAppName)
114 .write();
115 });
116
117 // Delete bld forder and bin folder
118 ['bld', 'bin', '*.user', '*.suo', 'MyTemplate.vstemplate'].forEach(function (file) {
119 shell.rm('-rf', path.join(projectPath, file));
120 });
121
122 events.emit('log', 'Windows project created with ' + pkg.name + '@' + pkg.version);
123 return Q.resolve();
124};
125
126function recursiveCreateDirectory (targetPath, previousPath) {
127 if (previousPath === targetPath) {
128 // Shouldn't ever happen because we're already in a created directory
129 // This is just here to prevent any potential infinite loop / stack overflow condition
130 console.warn('Could not create a directory because its root was never located.');
131 return;
132 }
133
134 var parent = path.join(targetPath, '..');
135 if (!fs.existsSync(parent)) {
136 recursiveCreateDirectory(parent, targetPath);
137 }
138
139 fs.mkdirSync(targetPath);
140}