|
| 1 | +var hook = require('nativescript-hook')(__dirname); |
| 2 | +hook.postinstall(); |
| 3 | + |
| 4 | +var fs = require('fs'); |
| 5 | +var path = require('path'); |
| 6 | + |
| 7 | +var projectDir = hook.findProjectDir(); |
| 8 | +if (projectDir) { |
| 9 | + createTsconfig(); |
| 10 | + createReferenceFile(); |
| 11 | + installTypescript(); |
| 12 | +} |
| 13 | + |
| 14 | +function createReferenceFile() { |
| 15 | + var referenceFilePath = path.join(projectDir, 'references.d.ts'), |
| 16 | + content = '/// <reference path="./node_modules/tns-core-modules/tns-core-modules.d.ts" /> Needed for autocompletion and compilation.'; |
| 17 | + |
| 18 | + if (!fs.existsSync(referenceFilePath)) { |
| 19 | + fs.appendFileSync(referenceFilePath, content); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function createTsconfig() { |
| 24 | + var tsconfigPath = path.join(projectDir, 'tsconfig.json'); |
| 25 | + var tsconfig = {}; |
| 26 | + |
| 27 | + if (fs.existsSync(tsconfigPath)) { |
| 28 | + try { |
| 29 | + tsconfig = JSON.parse(fs.readFileSync(tsconfigPath)) |
| 30 | + } catch (err) { |
| 31 | + console.warn('tsconfig.json: ' + err.toString()); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + tsconfig.compilerOptions = tsconfig.compilerOptions || { |
| 36 | + module: "commonjs", |
| 37 | + target: "es5", |
| 38 | + sourceMap: true, |
| 39 | + experimentalDecorators: true, |
| 40 | + noEmitHelpers: true, |
| 41 | + }; |
| 42 | + |
| 43 | + tsconfig.exclude = ['node_modules', 'platforms']; |
| 44 | + |
| 45 | + var coreModulesPath = 'node_modules/tns-core-modules/'; |
| 46 | + var coreModulesTypingsPath = 'node_modules/tns-core-modules/tns-core-modules.d.ts'; |
| 47 | + |
| 48 | + try { |
| 49 | + var coreModulesPackageJson = JSON.parse(fs.readFileSync(path.join(projectDir, coreModulesPath, 'package.json'))); |
| 50 | + if (coreModulesPackageJson.typings) { |
| 51 | + coreModulesTypingsPath = coreModulesPath + coreModulesPackageJson.typings; |
| 52 | + } |
| 53 | + } catch (err) { |
| 54 | + console.warn('tns-core-modules/package.json: ' + err.toString()); |
| 55 | + } |
| 56 | + |
| 57 | + fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 4)); |
| 58 | +} |
| 59 | + |
| 60 | +function getProjectTypeScriptVersion() { |
| 61 | + try { |
| 62 | + var packageJsonPath = path.join(projectDir, "package.json"), |
| 63 | + packageJsonContent = fs.readFileSync(packageJsonPath, "utf8"), |
| 64 | + jsonContent = JSON.parse(packageJsonContent); |
| 65 | + |
| 66 | + return (jsonContent.dependencies && jsonContent.dependencies.typescript) |
| 67 | + || (jsonContent.devDependencies && jsonContent.devDependencies.typescript); |
| 68 | + } catch(err) { |
| 69 | + console.error(err); |
| 70 | + return null; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function installTypescript() { |
| 75 | + var installedTypeScriptVersion = getProjectTypeScriptVersion(); |
| 76 | + if(installedTypeScriptVersion) { |
| 77 | + console.log("Project already targets TypeScript " + installedTypeScriptVersion); |
| 78 | + } else { |
| 79 | + require('child_process').exec('npm install --save-dev typescript', { cwd: projectDir }, function (err, stdout, stderr) { |
| 80 | + if (err) { |
| 81 | + console.warn('npm: ' + err.toString()); |
| 82 | + } |
| 83 | + process.stdout.write(stdout); |
| 84 | + process.stderr.write(stderr); |
| 85 | + }); |
| 86 | + } |
| 87 | +} |
0 commit comments