1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var Q = require('q');
|
21 | var fs = require('fs');
|
22 | var path = require('path');
|
23 | var shell = require('shelljs');
|
24 | var uuid = require('node-uuid');
|
25 | var events = require('cordova-common').events;
|
26 | var CordovaError = require('cordova-common').CordovaError;
|
27 | var AppxManifest = require('../../template/cordova/lib/AppxManifest');
|
28 | var pkg = require('../../package');
|
29 |
|
30 |
|
31 | module.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 |
|
40 | var packageName = (config && config.packageName()) || 'Cordova.Example';
|
41 | var appName = (config && config.name()) || 'CordovaAppProj';
|
42 |
|
43 |
|
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 |
|
58 | events.emit('verbose', 'Copying windows template project to ' + projectPath);
|
59 | shell.cp('-rf', path.join(root, 'template', '*'), projectPath);
|
60 |
|
61 |
|
62 | shell.cp('-rf', path.join(root, 'template/www/cordova.js'), path.join(projectPath, 'platform_www'));
|
63 |
|
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 |
|
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 |
|
73 | shell.cp('-rf', path.join(root, 'VERSION'), projectPath);
|
74 |
|
75 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
126 | function recursiveCreateDirectory (targetPath, previousPath) {
|
127 | if (previousPath === targetPath) {
|
128 |
|
129 |
|
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 | }
|