|
| 1 | +var path = require("path"); |
| 2 | +var fs = require("fs"); |
| 3 | +var childProcess = require("child_process"); |
| 4 | + |
| 5 | +var projectDir = path.dirname(path.dirname(__dirname)); |
| 6 | +var appDir = path.join(projectDir, "app"); |
| 7 | + |
| 8 | +var packageJsonPath = path.join(projectDir, "package.json"); |
| 9 | +var packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); |
| 10 | + |
| 11 | +var isAngular = Object.keys(packageJson.dependencies).filter(function (dependency) { |
| 12 | + return /^@angular\b/.test(dependency); |
| 13 | +}).length > 0; |
| 14 | + |
| 15 | +var isTypeScript = fs.existsSync(path.join(projectDir, "tsconfig.json")); |
| 16 | +if (isAngular) { |
| 17 | + isTypeScript = true; |
| 18 | +} |
| 19 | + |
| 20 | +function getProjectTemplates() { |
| 21 | + var templates = { |
| 22 | + "webpack.android.js.template": "webpack.android.js", |
| 23 | + "webpack.ios.js.template": "webpack.ios.js", |
| 24 | + }; |
| 25 | + |
| 26 | + if (isAngular) { |
| 27 | + templates["webpack.common.js.angular.template"] = "webpack.common.js"; |
| 28 | + templates["tsconfig.aot.json.template"] = "tsconfig.aot.json"; |
| 29 | + } else { |
| 30 | + templates["webpack.common.js.nativescript.template"] = "webpack.common.js"; |
| 31 | + } |
| 32 | + return templates; |
| 33 | +} |
| 34 | + |
| 35 | +function getAppTemplates() { |
| 36 | + var templates = { |
| 37 | + "vendor-platform.android.ts.template": tsOrJs("vendor-platform.android"), |
| 38 | + "vendor-platform.ios.ts.template": tsOrJs("vendor-platform.ios"), |
| 39 | + }; |
| 40 | + |
| 41 | + if (isAngular) { |
| 42 | + templates["vendor.ts.angular.template"] = tsOrJs("vendor"); |
| 43 | + } else { |
| 44 | + templates["vendor.ts.nativescript.template"] = tsOrJs("vendor"); |
| 45 | + } |
| 46 | + return templates; |
| 47 | +} |
| 48 | + |
| 49 | +function addProjectFiles() { |
| 50 | + var projectTemplates = getProjectTemplates(); |
| 51 | + Object.keys(projectTemplates).forEach(function(templateName) { |
| 52 | + var templateDestination = projectTemplates[templateName]; |
| 53 | + copyProjectTemplate(templateName, templateDestination); |
| 54 | + }); |
| 55 | + |
| 56 | + var appTemplates = getAppTemplates(); |
| 57 | + Object.keys(appTemplates).forEach(function(templateName) { |
| 58 | + var templateDestination = appTemplates[templateName]; |
| 59 | + copyAppTemplate(templateName, templateDestination); |
| 60 | + }); |
| 61 | +} |
| 62 | +exports.addProjectFiles = addProjectFiles; |
| 63 | + |
| 64 | +function removeProjectFiles() { |
| 65 | + var projectTemplates = getProjectTemplates(); |
| 66 | + Object.keys(projectTemplates).forEach(function(templateName) { |
| 67 | + var templateDestination = projectTemplates[templateName]; |
| 68 | + deleteProjectFile(templateDestination); |
| 69 | + }); |
| 70 | + |
| 71 | + var appTemplates = getAppTemplates(); |
| 72 | + Object.keys(appTemplates).forEach(function(templateName) { |
| 73 | + var templateDestination = appTemplates[templateName]; |
| 74 | + deleteAppFile(templateDestination); |
| 75 | + }); |
| 76 | +} |
| 77 | +exports.removeProjectFiles = removeProjectFiles; |
| 78 | + |
| 79 | +function getScriptTemplates() { |
| 80 | + return { |
| 81 | + "clean-[PLATFORM]": "tns clean-app [PLATFORM]", |
| 82 | + "prewebpack-[PLATFORM]": "npm run clean-[PLATFORM]", |
| 83 | + "webpack-[PLATFORM]": "webpack --config=webpack.[PLATFORM].js --progress", |
| 84 | + "prestart-[PLATFORM]-bundle": "npm run webpack-[PLATFORM]", |
| 85 | + "start-[PLATFORM]-bundle": "tns run [PLATFORM] --bundle --disable-npm-install", |
| 86 | + "prebuild-[PLATFORM]-bundle": "npm run webpack-[PLATFORM]", |
| 87 | + "build-[PLATFORM]-bundle": "tns build [PLATFORM] --bundle --disable-npm-install", |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +function addNpmScripts() { |
| 92 | + var scriptTemplates = getScriptTemplates(); |
| 93 | + Object.keys(scriptTemplates).forEach(function(templateName) { |
| 94 | + addPlatformScript(packageJson, templateName, scriptTemplates[templateName]); |
| 95 | + }); |
| 96 | +} |
| 97 | +exports.addNpmScripts = addNpmScripts; |
| 98 | + |
| 99 | +function removeNpmScripts() { |
| 100 | + var scriptTemplates = getScriptTemplates(); |
| 101 | + Object.keys(scriptTemplates).forEach(function(templateName) { |
| 102 | + removePlatformScripts(packageJson, templateName); |
| 103 | + }); |
| 104 | +} |
| 105 | +exports.removeNpmScripts = removeNpmScripts; |
| 106 | + |
| 107 | +function getProjectDependencies() { |
| 108 | + var dependencies = { |
| 109 | + "webpack": "~2.1.0-beta.27", |
| 110 | + "webpack-sources": "~0.1.3", |
| 111 | + "copy-webpack-plugin": "~3.0.1", |
| 112 | + "raw-loader": "~0.5.1", |
| 113 | + "nativescript-css-loader": "~0.26.0", |
| 114 | + "resolve-url-loader": "~1.6.0", |
| 115 | + "extract-text-webpack-plugin": "~2.0.0-beta.4", |
| 116 | + }; |
| 117 | + |
| 118 | + if (isAngular) { |
| 119 | + dependencies["@angular/compiler-cli"] = "2.4.3"; |
| 120 | + dependencies["@ngtools/webpack"] = "1.2.1"; |
| 121 | + dependencies["typescript"] = "^2.0.10"; |
| 122 | + dependencies["htmlparser2"] = "~3.9.2"; |
| 123 | + } else { |
| 124 | + dependencies["awesome-typescript-loader"] = "~3.0.0-beta.9"; |
| 125 | + } |
| 126 | + return dependencies; |
| 127 | +} |
| 128 | + |
| 129 | +function addProjectDependencies() { |
| 130 | + configureDevDependencies(packageJson, function (add) { |
| 131 | + var dependencies = getProjectDependencies(); |
| 132 | + Object.keys(dependencies).forEach(function(dependencyName) { |
| 133 | + add(dependencyName, dependencies[dependencyName]); |
| 134 | + }); |
| 135 | + }); |
| 136 | +} |
| 137 | +exports.addProjectDependencies = addProjectDependencies; |
| 138 | + |
| 139 | +function removeProjectDependencies() { |
| 140 | + configureDevDependencies(packageJson, function (add, remove) { |
| 141 | + var dependencies = getProjectDependencies(); |
| 142 | + Object.keys(dependencies).forEach(function(dependencyName) { |
| 143 | + remove(dependencyName); |
| 144 | + }); |
| 145 | + }); |
| 146 | +} |
| 147 | +exports.removeProjectDependencies = removeProjectDependencies; |
| 148 | + |
| 149 | + |
| 150 | +function addPlatformScript(packageJson, nameTemplate, commandTemplate) { |
| 151 | + if (!packageJson.scripts) { |
| 152 | + packageJson.scripts = {}; |
| 153 | + } |
| 154 | + |
| 155 | + var scripts = packageJson.scripts; |
| 156 | + ["android", "ios"].forEach(function (platform) { |
| 157 | + var name = nameTemplate.replace(/\[PLATFORM\]/g, platform); |
| 158 | + var command = commandTemplate.replace(/\[PLATFORM\]/g, platform); |
| 159 | + if (!scripts[name]) { |
| 160 | + console.log("Registering script: " + name); |
| 161 | + scripts[name] = command; |
| 162 | + } |
| 163 | + }); |
| 164 | +} |
| 165 | + |
| 166 | +function removePlatformScripts(packageJson, nameTemplate) { |
| 167 | + if (!packageJson.scripts) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + var scripts = packageJson.scripts; |
| 172 | + ["android", "ios"].forEach(function (platform) { |
| 173 | + var name = nameTemplate.replace(/\[PLATFORM\]/g, platform); |
| 174 | + console.log("Removing script: " + name); |
| 175 | + delete scripts[name]; |
| 176 | + }); |
| 177 | +} |
| 178 | + |
| 179 | +function configureDevDependencies(packageJson, adderCallback) { |
| 180 | + var pendingNpmInstall = false; |
| 181 | + if (!packageJson.devDependencies) { |
| 182 | + packageJson.devDependencies = {}; |
| 183 | + } |
| 184 | + var dependencies = packageJson.devDependencies; |
| 185 | + |
| 186 | + adderCallback(function (name, version) { |
| 187 | + if (!dependencies[name]) { |
| 188 | + dependencies[name] = version; |
| 189 | + console.info("Adding dev dependency: " + name + "@" + version); |
| 190 | + pendingNpmInstall = true; |
| 191 | + } else { |
| 192 | + console.info("Dev dependency: '" + name + "' already added. Leaving version: " + dependencies[name]); |
| 193 | + } |
| 194 | + }, function(name) { |
| 195 | + console.info("Removing dev dependency: " + name); |
| 196 | + delete dependencies[name]; |
| 197 | + }); |
| 198 | + |
| 199 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 200 | + |
| 201 | + if (pendingNpmInstall) { |
| 202 | + console.info("Installing new dependencies..."); |
| 203 | + //Run `npm install` after everything else. |
| 204 | + setTimeout(function () { |
| 205 | + var spawnArgs = []; |
| 206 | + if (/^win/.test(process.platform)) { |
| 207 | + spawnArgs = ["cmd.exe", ["/c", "npm", "install"]]; |
| 208 | + } else { |
| 209 | + spawnArgs = ["npm", ["install"]]; |
| 210 | + } |
| 211 | + spawnArgs.push({ cwd: projectDir, stdio: "inherit" }); |
| 212 | + var npm = childProcess.spawn.apply(null, spawnArgs); |
| 213 | + npm.on("close", function (code) { |
| 214 | + process.exit(code); |
| 215 | + }); |
| 216 | + }, 100); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +function tsOrJs(name) { |
| 221 | + if (isTypeScript) { |
| 222 | + return name + ".ts"; |
| 223 | + } else { |
| 224 | + return name + ".js"; |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +function copyProjectTemplate(templateName, projectPath) { |
| 229 | + var destinationPath = path.join(projectDir, projectPath); |
| 230 | + copyTemplate(templateName, destinationPath); |
| 231 | +} |
| 232 | + |
| 233 | +function deleteProjectFile(projectPath) { |
| 234 | + var destinationPath = path.join(projectDir, projectPath); |
| 235 | + console.log("Deleting file: " + destinationPath); |
| 236 | + fs.unlink(destinationPath); |
| 237 | +} |
| 238 | + |
| 239 | +function copyAppTemplate(templateName, appPath) { |
| 240 | + var destinationPath = path.join(appDir, appPath); |
| 241 | + copyTemplate(templateName, destinationPath); |
| 242 | +} |
| 243 | + |
| 244 | +function deleteAppFile(appPath) { |
| 245 | + var destinationPath = path.join(appDir, appPath); |
| 246 | + console.log("Deleting file: " + destinationPath); |
| 247 | + fs.unlink(destinationPath); |
| 248 | +} |
| 249 | + |
| 250 | +function copyTemplate(templateName, destinationPath) { |
| 251 | + var templatePath = path.join(__dirname, templateName); |
| 252 | + // Create destination file, only if not present. |
| 253 | + if (!fs.existsSync(destinationPath)) { |
| 254 | + console.log("Creating file: " + destinationPath); |
| 255 | + var content = fs.readFileSync(templatePath, "utf8"); |
| 256 | + fs.writeFileSync(destinationPath, content); |
| 257 | + } |
| 258 | +} |
0 commit comments