smartpath/ts/smartpath.transform.ts

42 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2018-07-21 21:26:11 +00:00
import plugins = require('./smartpath.plugins');
2016-03-12 07:24:05 +00:00
2016-03-20 17:59:30 +00:00
/* ------------------------------------------ *
* ------------ helpers --------------------- *
* ------------------------------------------ */
2016-09-30 15:08:09 +00:00
2018-07-21 21:26:11 +00:00
// checks a file
2016-09-30 15:08:09 +00:00
let makeAbsolute = function(localPathArg: string, baseArg?: string): string {
2018-07-21 21:26:11 +00:00
let absolutePath: string;
let alreadyAbsolute = plugins.path.isAbsolute(localPathArg);
if (baseArg && !alreadyAbsolute) {
absolutePath = plugins.path.join(baseArg, localPathArg);
} else if (!alreadyAbsolute) {
absolutePath = plugins.path.resolve(localPathArg);
} else {
absolutePath = localPathArg;
}
return absolutePath;
};
2016-03-12 07:24:05 +00:00
2016-03-20 17:59:30 +00:00
/* ------------------------------------------ *
* ------- export functions ----------------- *
* ------------------------------------------ */
2016-09-30 15:08:09 +00:00
export let toAbsolute = function(relativeArg: string | string[], baseArg?: string): any {
2018-07-21 21:26:11 +00:00
if (typeof relativeArg === 'string') {
return makeAbsolute(relativeArg, baseArg);
} else if (Array.isArray(relativeArg)) {
let relativeArray = relativeArg;
let absoluteArray: string[] = [];
for (let key in relativeArray) {
absoluteArray.push(makeAbsolute(relativeArray[key], baseArg));
2016-03-12 07:24:05 +00:00
}
2018-07-21 21:26:11 +00:00
return absoluteArray;
} else {
console.error(
'smartpath.absolute() could not make sense of the input. ' +
'Input is neither String nor Array'
);
return false;
}
};