nativescript-dev-assets
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -8,25 +8,58 @@ const fs = require('fs'); | ||
return new Promise((resolve, reject) => { | ||
if (utils.isAndroid(hookArgs)) { | ||
let androidAppAssetsFolderPath = [$projectData.platformsDir, "/android/src/main/assets/app/assets"].join(""); | ||
rmFolderRecursively(androidAppAssetsFolderPath).then(() => { | ||
$logger.info("Successfully removed useless android platform app assets"); | ||
transferAssetFiles($logger, $projectData, hookArgs).then(() => { | ||
console.log('everything ok (1)'); | ||
cleanAfterTransfer($logger, $projectData, hookArgs).then(() => { | ||
resolve(); | ||
}, (error) => { | ||
$logger.info("Error while deleting useless platform app assets: ", error); | ||
reject(); | ||
}); | ||
}) | ||
}); | ||
}, (error) => { | ||
reject(); | ||
}); | ||
}; | ||
function transferAssetFiles($logger, $projectData, hookArgs) { | ||
return new Promise(function (resolve, reject) { | ||
//source | ||
const pathToAppAssets = path.join($projectData.appDirectoryPath, "/assets"); | ||
if (fs.existsSync(pathToAppAssets)) { | ||
//target | ||
let pathToPlatformAssets = utils.getPathToPlatformAssets(hookArgs, $projectData); | ||
if (fs.existsSync(pathToPlatformAssets)) { | ||
utils.copyFolderRecursively(pathToAppAssets, pathToPlatformAssets).then(() => { | ||
$logger.info('Successfully transferred all assets files.'); | ||
resolve(); | ||
}, (error) => { | ||
$logger.info('Error while transferring assets files. Error: ', error); | ||
reject(); | ||
}); | ||
} else { | ||
$logger.info(["No assets folder found in platform at: ", pathToPlatformAssets].join("")); | ||
reject(); | ||
} | ||
} else { | ||
let iosAppAssetsFolderPath = [$projectData.platformsDir, "/ios/demo/app/assets"].join(""); | ||
rmFolderRecursively(iosAppAssetsFolderPath).then((error) => { | ||
if (error) { | ||
$logger.info("Error while deleting useless ios platform app assets: ", error); | ||
} else { | ||
$logger.info("Successfully removed useless ios platform app assets"); | ||
resolve(); | ||
} | ||
}); | ||
$logger.info(["No assets folder found in your app folder at: ", pathToAppAssets].join("")); | ||
reject(); | ||
} | ||
}); | ||
}; | ||
} | ||
function cleanAfterTransfer($logger, $projectData, hookArgs) { | ||
return new Promise((resolve, reject) => { | ||
console.log('everything ok (2)'); | ||
let platformAppAssetsFolderPath = utils.getPathToAppPlatformAssets(hookArgs, $projectData); | ||
console.log('everything ok (3)'); | ||
console.log(platformAppAssetsFolderPath); | ||
rmFolderRecursively(platformAppAssetsFolderPath).then(() => { | ||
$logger.info("Successfully removed useless platform app assets"); | ||
resolve(); | ||
}, (error) => { | ||
$logger.info("Error while deleting useless platform app assets: ", error); | ||
reject(); | ||
}); | ||
}); | ||
} | ||
function rmFolderRecursively(folderPath) { | ||
@@ -33,0 +66,0 @@ return new Promise((resolve, reject) => { |
135
lib/utils.js
const fs = require('fs'); | ||
const path = require('path'); | ||
const each = require('async-each'); | ||
@@ -11,2 +13,34 @@ module.exports = { | ||
}, | ||
getNativeScriptVersion($projectData) { | ||
// [3, 4, 1] | ||
return $projectData.$projectHelper.$staticConfig.version.split("."); | ||
}, | ||
getPathToPlatformAssets(hookArgs, $projectData) { | ||
let pathToPlatformAssets; | ||
if (this.isAndroid(hookArgs)) { | ||
let currentNVersion = this.getNativeScriptVersion($projectData); | ||
if (currentNVersion[0] >= 3 && currentNVersion[1] >= 4) { | ||
pathToPlatformAssets = path.join($projectData.platformsDir, "/android/app/src/main/assets"); | ||
} else { | ||
pathToPlatformAssets = path.join($projectData.platformsDir, "/android/src/main/assets"); | ||
} | ||
} else { | ||
pathToPlatformAssets = path.join($projectData.platformsDir, "ios/", $projectData.projectName, "/Resources"); | ||
} | ||
return pathToPlatformAssets; | ||
}, | ||
getPathToAppPlatformAssets(hookArgs, $projectData) { | ||
let platformAppAssetsFolderPath, | ||
pathToPlatformAssets = this.getPathToPlatformAssets(hookArgs, $projectData); | ||
if (this.isAndroid(hookArgs)) { | ||
//platformAppAssetsFolderPath = [$projectData.platformsDir, "/android/src/main/assets/app/assets"].join(""); | ||
platformAppAssetsFolderPath = path.join(pathToPlatformAssets, "app/assets"); | ||
} else { | ||
pathToPlatformAssets = pathToPlatformAssets.split("/"); | ||
pathToPlatformAssets.pop(); | ||
pathToPlatformAssets = pathToPlatformAssets.join("/"); | ||
platformAppAssetsFolderPath = [pathToPlatformAssets, "/app/assets"].join(""); | ||
} | ||
return platformAppAssetsFolderPath; | ||
}, | ||
isFolder(folderPath) { | ||
@@ -33,3 +67,104 @@ return new Promise((resolve, reject) => { | ||
}); | ||
}, | ||
copyFile(sourceFile, targetFile) { | ||
return new Promise((resolve, reject) => { | ||
fs.exists(targetFile, (exists) => { | ||
if (!exists) { | ||
fs.readFile(sourceFile, (error, data) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
fs.writeFile(targetFile, data, (error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
}); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}, | ||
copyFolder(folderPath) { | ||
return new Promise((resolve, reject) => { | ||
fs.exists(folderPath, (exists) => { | ||
if (!exists) { | ||
fs.mkdir(folderPath, (error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}, | ||
iterateOverFolder(files, sourceFolder, targetFolder) { | ||
return new Promise((resolve, reject) => { | ||
each(files, (file, next) => { | ||
let currentSource = path.join(sourceFolder, file), | ||
currentTarget = path.join(targetFolder, file); | ||
this.isFolder(currentSource).then((result) => { | ||
if (result) { | ||
this.copyFolderRecursively(currentSource, currentTarget).then(() => { | ||
next(); | ||
}, (error) => { | ||
reject(error); | ||
}); | ||
} else { | ||
this.copyFile(currentSource, currentTarget).then(() => { | ||
next(); | ||
}, (error) => { | ||
reject(error); | ||
}); | ||
} | ||
}, (error) => { | ||
reject(error); | ||
}); | ||
}, (error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}, | ||
copyFolderRecursively(sourceFolder, targetFolder) { | ||
return new Promise((resolve, reject) => { | ||
this.isFolder(sourceFolder).then((result) => { | ||
if (result) { | ||
this.copyFolder(targetFolder).then(() => { | ||
this.getFolderFiles(sourceFolder).then((files) => { | ||
this.iterateOverFolder(files, sourceFolder, targetFolder).then(() => { | ||
resolve(); | ||
}, (error) => { | ||
reject(error); | ||
}) | ||
}, (error) => { | ||
reject(error); | ||
}); | ||
}, (error) => { | ||
reject(error); | ||
}); | ||
} else { | ||
this.copyFile(sourceFolder, targetFolder).then(() => { | ||
resolve(); | ||
}, (error) => { | ||
reject(error); | ||
}); | ||
} | ||
}, (error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
} |
{ | ||
"name": "nativescript-dev-assets", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Simple hook allowing you to sync static files into the assets folder inside your Android app.", | ||
"scripts": { | ||
"test": "cd demo && tns install ../ && tns prepare android", | ||
"test.android": "cd demo && tns install ../ && tns prepare android", | ||
"test.ios": "cd demo && tns install ../ && tns prepare ios", | ||
"postinstall": "node postinstall.js", | ||
@@ -45,7 +46,2 @@ "preuninstall": "node preuninstall.js" | ||
{ | ||
"type": "before-prepare", | ||
"script": "lib/before-prepare.js", | ||
"inject": true | ||
}, | ||
{ | ||
"type": "after-prepare", | ||
@@ -52,0 +48,0 @@ "script": "lib/after-prepare.js", |
@@ -0,6 +1,12 @@ | ||
[![npm](https://img.shields.io/npm/v/nativescript-dev-assets.svg)](https://www.npmjs.com/package/nativescript-dev-assets) | ||
[![npm](https://img.shields.io/npm/dt/nativescript-dev-assets.svg?label=npm%20downloads)](https://www.npmjs.com/package/nativescript-dev-assets) | ||
[![twitter: @_rhanb](https://img.shields.io/badge/twitter-%40rhanb-2F98C1.svg)](https://twitter.com/_rhanb) | ||
[![NPM](https://nodei.co/npm/nativescript-dev-assets.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/nativescript-dev-assets/) | ||
# nativescript-dev-assets | ||
This simple `before-prepare` hook will make an `assets` folder in your app folder, all files in it will be synced in the `platforms/android/src/main/assets` folder. | ||
This simple `after-prepare` hook will make an `assets` folder in your app folder, all files in it will be synced in the `platforms/android/app/src/main/assets` folder for Android and in the `platforms/ios/appName/Resources` folder for iOS. | ||
Basically to fix this [issue](https://github.com/NativeScript/android-runtime/issues/700) and to ease [nativescript-lottie](https://github.com/bradmartin/nativescript-lottie) usage under android. | ||
Basically to fix this [issue](https://github.com/NativeScript/android-runtime/issues/700) and to ease [nativescript-lottie](https://github.com/bradmartin/nativescript-lottie). | ||
@@ -7,0 +13,0 @@ # Installation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13973
316
20
3
7
1