Skip to content

Commit

Permalink
chore: migrate template to version 2 (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgpetrov authored and manoldonev committed Jan 9, 2019
1 parent 4065b5e commit 9a93934
Show file tree
Hide file tree
Showing 111 changed files with 95 additions and 373 deletions.
2 changes: 1 addition & 1 deletion packages/template-master-detail-ts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ npm-debug.log
*.js
*.css
!tools/*.js
hooks/
!hooks/**/*.js
lib/
node_modules/
platforms/
Expand Down
18 changes: 18 additions & 0 deletions packages/template-master-detail-ts/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
npm-debug.log
.DS_Store

*.js.map
*.js
*.css
!tools/*.js
!hooks/**/*.js
lib/
node_modules/
platforms/
tmp/
typings/
.idea
.cloud
.project

tools/assets/
File renamed without changes.
6 changes: 6 additions & 0 deletions packages/template-master-detail-ts/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"main": "app.js",
"android": {
"v8Flags": "--expose_gc"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,92 @@ const fs = require("fs");
const path = require("path");
const exec = require("child_process").exec;

console.log("Postinstall script running...");

getPackageJson()
.then((packageJsonData) => Promise.all([
// results of glob parameter expansion can vary depending on shell, and its configuration
// quote the parameter to use node glob syntax (using double quotes if you need it to run in Windows)
addScriptCommand(packageJsonData, {
commandName: "lint",
command: "tslint \"app/**/*.ts\"",
message: "Updating package.json scripts for linting..."
}),
updateFirebaseConfigAppId(packageJsonData)
]))
.catch((err) => {
console.error(err);
console.log("after-createProject script running...");

const srcGitignore = "dot.gitignore";
const destGitignore = ".gitignore";
let appRootFolder;

findAppRootFolder()
.then(() => {
return copyFile(srcGitignore, destGitignore);
})
.then(() => {
const packageJsonPath = path.join(appRootFolder, "package.json");
const packageJson = require(packageJsonPath);

if (!packageJson) {
throw new Error("package.json file not found");
}

return updateFirebaseConfigAppId(packageJson);
})
.then(() => {
// Remove tools folder including this script
console.log("Removing tools directory...");
const toolsDir = path.join(appRootFolder, 'tools');
deleteFolderSync(toolsDir);

console.log("Removing after-createProject...");
deleteFolderSync(__dirname);
})
.catch((err) => {
console.log(err);
});

function getPackageJson() {
return getAppRootFolder()
.then((appRootFolder) => {
const packageJsonPath = path.join(appRootFolder, "package.json");
const packageJson = require(packageJsonPath);
function copyFile(srcFilename, destFilename = srcFilename) {
return new Promise((resolve, reject) => {
const sourcePath = path.join(appRootFolder, 'tools', srcFilename);
const destPath = path.join(appRootFolder, destFilename);

if (!packageJson) {
throw new Error("package.json file not found");
console.log(`Creating ${path.resolve(destPath)}...`);
fs.rename(sourcePath, destPath, (err) => {
if (err) {
return reject(err);
}

return {
packageJson,
packageJsonPath
};
resolve();
});
});
}

function getAppRootFolder() {
function findAppRootFolder() {
return new Promise((resolve, reject) => {
// npm prefix returns the closest parent directory to contain a package.json file
exec("cd .. && npm prefix", (err, stdout) => {
exec(`cd ${__dirname} && npm prefix`, (err, stdout) => {
if (err) {
return reject(err);
}

resolve(stdout.toString().trim());
appRootFolder = stdout.toString().trim();
resolve();
});
});
}

function addScriptCommand(packageJsonData, options) {
return new Promise((resolve, reject) => {
if (options.message) {
console.log(options.message);
}

const { packageJson, packageJsonPath } = packageJsonData;
if (!packageJson.scripts) {
packageJson.scripts = {};
}

packageJson.scripts[options.commandName] = options.command;
function deleteFolderSync(folderPath) {
if (fs.statSync(folderPath).isDirectory()) {
fs.readdirSync(folderPath).forEach((file) => {
const content = path.join(folderPath, file);
const contentDirs = fs.statSync(content).isDirectory();

const updatedContent = JSON.stringify(packageJson);
fs.writeFile(packageJsonPath, updatedContent, (err) => {
if (err) {
return reject(err);
if (contentDirs) {
deleteFolderSync(content);
}
else {
fs.unlinkSync(content);
}

resolve();
});
});

fs.rmdirSync(folderPath);
}
}

function updateFirebaseConfigAppId(packageJsonData) {
function updateFirebaseConfigAppId(packageJson) {
console.log("Updating google services configuration for firebase...");

const googleServicesJsonPath = path.join(process.cwd(), "App_Resources", "Android", "google-services.json");
const googleServiceInfoPlistPath = path.join(process.cwd(), "App_Resources", "iOS", "GoogleService-Info.plist");
const googleServicesJsonPath = path.join(appRootFolder, "app", "App_Resources", "Android", "google-services.json");
const googleServiceInfoPlistPath = path.join(appRootFolder, "app", "App_Resources", "iOS", "GoogleService-Info.plist");

const packageJson = packageJsonData.packageJson;
if (!packageJson.nativescript || !packageJson.nativescript.id) {
return Promise.reject(new Error("cannot find nativescript node in package.json file"));
}
Expand Down Expand Up @@ -113,22 +116,4 @@ function replaceAppId(filePath, appId) {
});
});
});
}

function deleteFolderSync(folderPath) {
if (fs.statSync(folderPath).isDirectory()) {
fs.readdirSync(folderPath).forEach((file) => {
const content = path.join(folderPath, file);
const contentDirs = fs.statSync(content).isDirectory();

if (contentDirs) {
deleteFolderSync(content);
}
else {
fs.unlinkSync(content);
}
});

fs.rmdirSync(folderPath);
}
}
}
20 changes: 13 additions & 7 deletions packages/template-master-detail-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"main": "app.js",
"nativescript": {
"id": "org.nativescript.MasterDetail",
"templateVersion": "v2",
"tns-android": {
"version": "4.1.3"
},
"tns-ios": {
"version": "4.1.1"
}
},
"name": "tns-template-master-detail-ts",
"displayName": "Master-Detail with Firebase",
"templateType": "App template",
Expand All @@ -26,14 +35,15 @@
"bugs": {
"url": "https://github.com/NativeScript/NativeScript/issues"
},
"android": {
"v8Flags": "--expose_gc"
"scripts": {
"lint": "tslint \"app/**/*.ts\""
},
"dependencies": {
"nativescript-imagepicker": "~6.0.0",
"nativescript-plugin-firebase": "~6.0.0",
"nativescript-theme-core": "~1.0.4",
"nativescript-ui-listview": "~3.5.0",
"tns-core-modules": "~4.1.0",
"rxjs": "~6.1.0"
},
"devDependencies": {
Expand All @@ -42,9 +52,5 @@
"nativescript-dev-webpack": "~0.14.0",
"tslint": "~5.10.0",
"typescript": "~2.7.2"
},
"scripts": {
"preinstall": "node tools/preinstall.js",
"postinstall": "node tools/postinstall.js"
}
}
Loading

0 comments on commit 9a93934

Please sign in to comment.