Skip to content
Open
28 changes: 22 additions & 6 deletions bin/pcsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ program
.description('compare all local and remote files and folders')
.option('-r, --regexp <regexp>', 'handle files matching the provided regular expression')
.option('-e, --ext <extensions>', 'handle files with provided extensions')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runCompAll);

program
.command('diff <filePath>')
.description('show line-by-line diff of the remote and local files at filePath')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runDiff);

program
Expand All @@ -26,6 +28,7 @@ program
.option('-r, --regexp <regexp>', 'handle files matching the provided regular expression')
.option('-e, --ext <extensions>', 'handle files with provided extensions')
.option('-y, --yes', 'Automatically answer "yes" to any prompts that might print on the command line.')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runOverwriteAllLocal);

program
Expand All @@ -34,48 +37,56 @@ program
.option('-r, --regexp <regexp>', 'handle files matching the provided regular expression')
.option('-e, --ext <extensions>', 'handle files with provided extensions')
.option('-y, --yes', 'Automatically answer "yes" to any prompts that might print on the command line.')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runOverwriteAllRemote);

program
.command('pull <filePath>')
.description('download remote file, creating local folders if needed')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runDownloadSingle);

program
.command('push <filePath>')
.description('upload local file, creating remote folders if needed')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runUploadSingle);

program
.command('rename <oldPath> <newPath>')
.description('rename remote file or folder, change its parent folder if needed')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runRename);

program
.command('rm <filePath>')
.description('remove remote file or folder')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runDelete);

program
.command('parseIgnore')
.description('list assets matched by pcignore.txt')
.option('-p, --profile <profile>', 'Use the profile specified in the config file')
.action(runParse);

function runCompAll(cmdObj) {
CUtils.handleForceRegOpts(cmdObj);

CUtils.handleProfileOpts(cmdObj);
CUtils.wrapUserErrors(() => {
return SyncUtils.reportDiffAll();
});
}

function runDiff(filePath) {
function runDiff(filePath, cmdObj) {
CUtils.handleProfileOpts(cmdObj);
CUtils.wrapUserErrors(() => {
return SCUtils.diffSingleFile(filePath);
});
}

function runOverwriteAllLocal(cmdObj) {
CUtils.handleProfileOpts(cmdObj);
CUtils.handleForceRegOpts(cmdObj);

const cb = function () {
Expand All @@ -86,6 +97,7 @@ function runOverwriteAllLocal(cmdObj) {
}

function runOverwriteAllRemote(cmdObj) {
CUtils.handleProfileOpts(cmdObj);
CUtils.handleForceRegOpts(cmdObj);

const cb = function () {
Expand All @@ -95,29 +107,33 @@ function runOverwriteAllRemote(cmdObj) {
return cmdObj.yes ? cb() : SyncUtils.compareAndPrompt(cb);
}

function runDownloadSingle(filePath) {
function runDownloadSingle(filePath, cmdObj) {
CUtils.handleProfileOpts(cmdObj);
CUtils.setForceEnv(filePath);

CUtils.wrapUserErrors(() => {
return SCUtils.downloadSingleFile(filePath);
});
}

function runUploadSingle(filePath) {
function runUploadSingle(filePath, cmdObj) {
CUtils.handleProfileOpts(cmdObj);
CUtils.setForceEnv(filePath);

CUtils.wrapUserErrors(() => {
return SCUtils.uploadSingleFile(filePath);
});
}

function runRename(oldPath, newPath) {
function runRename(oldPath, newPath, cmdObj) {
CUtils.handleProfileOpts(cmdObj);
CUtils.wrapUserErrors(() => {
return SCUtils.renameItem(oldPath, newPath);
});
}

function runDelete(filePath) {
function runDelete(filePath, cmdObj) {
CUtils.handleProfileOpts(cmdObj);
CUtils.setForceEnv(filePath);

CUtils.wrapUserErrors(() => {
Expand Down
4 changes: 2 additions & 2 deletions bin/pcwatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ const CacheUtils = require('../src/utils/cache-utils.js');
const LocalTraversal = require('../src/utils/local-traversal.js');

program.option('-f, --force', 'skip local/remote equality check');
program.option('-p, --profile <profile>', 'Use the profile specified in the config file')

program.parse(process.argv);

async function run() {
CUtils.handleProfileOpts(program.opts());
if (!program.opts().force) {
await CUtils.wrapUserErrors(() => SyncUtils.errorIfDifferent(true));

await CUtils.wrapUserErrors(SyncUtils.errorIfMultWatch);
}

await startWatcher();
}

Expand Down
5 changes: 5 additions & 0 deletions src/utils/common-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ const CUtils = {
CUtils.checkSetEnv('PLAYCANVAS_FORCE_REG', v);
},

handleProfileOpts: function (cmdObj) {
const v = cmdObj.profile || '';
CUtils.checkSetEnv('PLAYCANVAS_PROFILE', v);
},

extToReg: function (extensions) {
let a = extensions.split(',');

Expand Down
26 changes: 23 additions & 3 deletions src/utils/config-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const fs = require('fs');
const PathUtils = require('./path-utils.js');

const HOME_CONFIG_FILE = '.pcconfig';
const TARGET_CONFIG_FILE = 'pcconfig.json';
const TARGET_CONFIG_FILE = 'pcconfig';
const CONFIG_FILE_EXT = 'json';
const PROFILE_KEY = 'PLAYCANVAS_PROFILE';

const requiredFields = [
'PLAYCANVAS_API_KEY',
Expand Down Expand Up @@ -68,7 +70,7 @@ class ConfigVars {

await this.checkPrepTarg();

this.fromConfigFile(this.result.PLAYCANVAS_TARGET_DIR, TARGET_CONFIG_FILE);
this.fromTargetConfigFile(this.result.PLAYCANVAS_TARGET_DIR, TARGET_CONFIG_FILE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

I'm just thinking, it might be clearer for the user to specify the config filename directly (instead of a profile name).

Then the only code change here would be to use that filename instead of TARGET_CONFIG_FILE.

This way, the user knows exactly which file is being referenced and they can name the file however they like.

What do you think @figo2264?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, the main idea is to allow multiple config file on a single project and I adopted the concept from spring profile.

So, what you meant is just explicitly point out the config file name like what we normally do in typescript config, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'm imagining something like:

--config='pcconfig-dev.json'

Which would load settings from 'pcconfig-dev.json' instead of 'pcconfig.json'.


await this.addSubdirToTarget();

Expand All @@ -79,10 +81,28 @@ class ConfigVars {

fromConfigFile(start, name) {
start = start || '';
let p = path.join(start, name);
if (!fs.existsSync(p)) {
start = process.cwd();
p = path.join(start, name);
}
const h = CUtils.jsonFileToMap(p);
this.fromEnvOrMap(h);
}

fromTargetConfigFile(start, name) {
const originalName = `${name}.${CONFIG_FILE_EXT}`;
name = process.env[PROFILE_KEY] ? `${name}-${process.env[PROFILE_KEY]}.${CONFIG_FILE_EXT}` : `${name}.${CONFIG_FILE_EXT}`;
let p = path.join(start, name);
if (!fs.existsSync(p)) {
p = path.join(process.cwd(), name);
start = process.cwd();
p = path.join(start, name);
if (!fs.existsSync(p)) {
p = path.join(start, originalName);
if (!fs.existsSync(p)) {
console.log("Couldn't find either the default config file [%s] or the profile-specific config file [%s] from %s", originalName, name, start);
}
}
}
const h = CUtils.jsonFileToMap(p);
this.fromEnvOrMap(h);
Expand Down