diff --git a/README.md b/README.md index ca74056..8148971 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,16 @@ and environment variables. can also set `PLAYCANVAS_USE_CWD_AS_TARGET` to `1` in `.pcconfig` to use your current working directory as your target. +For some workflows, it may be necessary to +keep the `pcconfig.json` file at the top level +in the target directory, but treat one of its subdirectories as +the root of the local file hierarchy. In such cases +`PLAYCANVAS_TARGET_SUBDIR` needs to be provided, e.g. + +``` +"PLAYCANVAS_TARGET_SUBDIR": "src" +``` + Backslash characters should be written as `\\` (escaped). # Files and Folders to Exclude diff --git a/src/utils/common-utils.js b/src/utils/common-utils.js index a21872b..72e2f8f 100644 --- a/src/utils/common-utils.js +++ b/src/utils/common-utils.js @@ -271,6 +271,19 @@ const CUtils = { waitMs: function (ms) { return new Promise(resolve => setTimeout(resolve, ms)); + }, + + checkTargetExists: async function(fullPath) { + const stat = await PathUtils.fsWrap('stat', fullPath); + + const good = stat && stat.isDirectory; + + if (!good) { + const s = `Error: could not find target directory: ${fullPath}. ` + + 'Check capitalization.'; + + CUtils.throwFtError(s); + } } }; diff --git a/src/utils/config-vars.js b/src/utils/config-vars.js index bd72d42..f246073 100644 --- a/src/utils/config-vars.js +++ b/src/utils/config-vars.js @@ -18,6 +18,7 @@ const requiredFields = [ const optionalFields = [ 'PLAYCANVAS_USE_CWD_AS_TARGET', + 'PLAYCANVAS_TARGET_SUBDIR', 'PLAYCANVAS_INCLUDE_REG', 'PLAYCANVAS_FORCE_REG', 'PLAYCANVAS_DRY_RUN', @@ -67,6 +68,8 @@ class ConfigVars { this.fromConfigFile(this.result.PLAYCANVAS_TARGET_DIR, TARGET_CONFIG_FILE); + await this.addSubdirToTarget(); + this.fromDefaults(); requiredFields.forEach(this.checkRequired, this); @@ -93,14 +96,9 @@ class ConfigVars { CUtils.throwFtError('Error: do not use the playcanvas-sync directory as target'); } - const stat = await PathUtils.fsWrap('stat', s); - - if (stat && stat.isDirectory) { - this.result.PLAYCANVAS_TARGET_DIR = s; + await CUtils.checkTargetExists(s); - } else { - CUtils.throwFtError(`Error: could not find target directory: ${s}. Check capitalization.`); - } + this.result.PLAYCANVAS_TARGET_DIR = s; } fromEnvOrMap(h) { @@ -121,6 +119,21 @@ class ConfigVars { } } + async addSubdirToTarget() { + let s = this.result.PLAYCANVAS_TARGET_SUBDIR; + + if (s) { + s = PathUtils.rmLastSlash(s); + + this.result.PLAYCANVAS_TARGET_DIR = path.join( + this.result.PLAYCANVAS_TARGET_DIR, + s + ); + + await CUtils.checkTargetExists(this.result.PLAYCANVAS_TARGET_DIR); + } + } + makeReg(field) { const v = this.result[field];