|
| 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 addProjectFiles() { |
| 21 | + copyProjectTemplate("webpack.android.js.template", "webpack.android.js"); |
| 22 | + copyProjectTemplate("webpack.ios.js.template", "webpack.ios.js"); |
| 23 | + |
| 24 | + if (isAngular) { |
| 25 | + copyProjectTemplate("webpack.common.js.angular.template", "webpack.common.js"); |
| 26 | + copyProjectTemplate("tsconfig.aot.json.template", "tsconfig.aot.json"); |
| 27 | + } else { |
| 28 | + copyProjectTemplate("webpack.common.js.nativescript.template", "webpack.common.js"); |
| 29 | + } |
| 30 | + |
| 31 | + copyAppTemplate("vendor-platform.android.ts.template", tsOrJs("vendor-platform.android")); |
| 32 | + copyAppTemplate("vendor-platform.ios.ts.template", tsOrJs("vendor-platform.ios")); |
| 33 | + if (isAngular) { |
| 34 | + copyAppTemplate("vendor.ts.angular.template", tsOrJs("vendor")); |
| 35 | + } else { |
| 36 | + copyAppTemplate("vendor.ts.nativescript.template", tsOrJs("vendor")); |
| 37 | + } |
| 38 | +} |
| 39 | +exports.addProjectFiles = addProjectFiles; |
| 40 | + |
| 41 | +function addNpmScripts() { |
| 42 | + addPlatformScript(packageJson, "clean-[PLATFORM]", "tns clean-app [PLATFORM]"); |
| 43 | + addPlatformScript(packageJson, "prewebpack-[PLATFORM]", "npm run clean-[PLATFORM]"); |
| 44 | + addPlatformScript(packageJson, "webpack-[PLATFORM]", "webpack --config=webpack.[PLATFORM].js --progress"); |
| 45 | + addPlatformScript(packageJson, "prestart-[PLATFORM]-bundle", "npm run webpack-[PLATFORM]"); |
| 46 | + addPlatformScript(packageJson, "start-[PLATFORM]-bundle", "tns run [PLATFORM] --bundle --disable-npm-install"); |
| 47 | + addPlatformScript(packageJson, "prebuild-[PLATFORM]-bundle", "npm run webpack-[PLATFORM]"); |
| 48 | + addPlatformScript(packageJson, "build-[PLATFORM]-bundle", "tns build [PLATFORM] --bundle --disable-npm-install"); |
| 49 | +} |
| 50 | +exports.addNpmScripts = addNpmScripts; |
| 51 | + |
| 52 | +function addProjectDependencies() { |
| 53 | + configureDevDependencies(packageJson, function (add) { |
| 54 | + add("webpack", "~2.1.0-beta.27"); |
| 55 | + add("webpack-sources", "~0.1.3"); |
| 56 | + add("copy-webpack-plugin", "~3.0.1"); |
| 57 | + add("raw-loader", "~0.5.1"); |
| 58 | + add("nativescript-css-loader", "~0.26.0"); |
| 59 | + add("resolve-url-loader", "~1.6.0"); |
| 60 | + add("extract-text-webpack-plugin", "~2.0.0-beta.4"); |
| 61 | + |
| 62 | + if (isAngular) { |
| 63 | + add("@angular/compiler-cli", "2.4.3"); |
| 64 | + add("@ngtools/webpack", "1.2.1"); |
| 65 | + add("typescript", "^2.0.10"); |
| 66 | + add("htmlparser2", "~3.9.2"); |
| 67 | + } else { |
| 68 | + add("awesome-typescript-loader", "~3.0.0-beta.9"); |
| 69 | + } |
| 70 | + }); |
| 71 | +} |
| 72 | +exports.addProjectDependencies = addProjectDependencies; |
| 73 | + |
| 74 | + |
| 75 | +function addPlatformScript(packageJson, nameTemplate, commandTemplate) { |
| 76 | + if (!packageJson.scripts) { |
| 77 | + packageJson.scripts = {}; |
| 78 | + } |
| 79 | + |
| 80 | + var scripts = packageJson.scripts; |
| 81 | + ["android", "ios"].forEach(function (platform) { |
| 82 | + var name = nameTemplate.replace(/\[PLATFORM\]/g, platform); |
| 83 | + var command = commandTemplate.replace(/\[PLATFORM\]/g, platform); |
| 84 | + if (!scripts[name]) { |
| 85 | + scripts[name] = command; |
| 86 | + console.log("Registering script: " + name); |
| 87 | + } |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function configureDevDependencies(packageJson, adderCallback) { |
| 92 | + var pendingNpmInstall = false; |
| 93 | + if (!packageJson.devDependencies) { |
| 94 | + packageJson.devDependencies = {}; |
| 95 | + } |
| 96 | + var dependencies = packageJson.devDependencies; |
| 97 | + |
| 98 | + adderCallback(function (name, version) { |
| 99 | + if (!dependencies[name]) { |
| 100 | + dependencies[name] = version; |
| 101 | + console.info("Adding dev dependency: " + name + "@" + version); |
| 102 | + pendingNpmInstall = true; |
| 103 | + } else { |
| 104 | + console.info("Dev dependency: '" + name + "' already added. Leaving version: " + dependencies[name]); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 109 | + |
| 110 | + if (pendingNpmInstall) { |
| 111 | + console.info("Installing new dependencies..."); |
| 112 | + //Run `npm install` after everything else. |
| 113 | + setTimeout(function () { |
| 114 | + var spawnArgs = []; |
| 115 | + if (/^win/.test(process.platform)) { |
| 116 | + spawnArgs = ["cmd.exe", ["/c", "npm", "install"]]; |
| 117 | + } else { |
| 118 | + spawnArgs = ["npm", ["install"]]; |
| 119 | + } |
| 120 | + spawnArgs.push({ cwd: projectDir, stdio: "inherit" }); |
| 121 | + var npm = childProcess.spawn.apply(null, spawnArgs); |
| 122 | + npm.on("close", function (code) { |
| 123 | + process.exit(code); |
| 124 | + }); |
| 125 | + }, 100); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function tsOrJs(name) { |
| 130 | + if (isTypeScript) { |
| 131 | + return name + ".ts"; |
| 132 | + } else { |
| 133 | + return name + ".js"; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +function copyProjectTemplate(templateName, projectPath) { |
| 138 | + var destinationPath = path.join(projectDir, projectPath); |
| 139 | + copyTemplate(templateName, destinationPath); |
| 140 | +} |
| 141 | + |
| 142 | +function copyAppTemplate(templateName, appPath) { |
| 143 | + var destinationPath = path.join(appDir, appPath); |
| 144 | + copyTemplate(templateName, destinationPath); |
| 145 | +} |
| 146 | + |
| 147 | +function copyTemplate(templateName, destinationPath) { |
| 148 | + var templatePath = path.join(__dirname, templateName); |
| 149 | + // Create destination file, only if not present. |
| 150 | + if (!fs.existsSync(destinationPath)) { |
| 151 | + console.log("Creating: " + destinationPath); |
| 152 | + var content = fs.readFileSync(templatePath, "utf8"); |
| 153 | + fs.writeFileSync(destinationPath, content); |
| 154 | + } |
| 155 | +} |
0 commit comments