smartpath/ts/smartpath.transform.ts

50 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2022-03-14 12:26:48 +00:00
import * as plugins from './smartpath.plugins.js';
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
2024-04-12 12:59:49 +00:00
/**
* takes a path and makes it absolute
* @param localPathArg
* @param baseArg
* @returns
*/
2022-03-16 10:35:25 +00:00
export const makeAbsolute = (localPathArg: string, baseArg?: string): string => {
2018-07-21 21:26:11 +00:00
let absolutePath: string;
let alreadyAbsolute = plugins.path.isAbsolute(localPathArg);
2024-04-12 12:59:49 +00:00
if (!alreadyAbsolute && baseArg && !baseArg.startsWith('.')) {
2018-07-21 21:26:11 +00:00
absolutePath = plugins.path.join(baseArg, localPathArg);
} else if (!alreadyAbsolute) {
2024-04-12 12:59:49 +00:00
if (baseArg) {
plugins.path.join(baseArg, localPathArg);
}
2018-07-21 21:26:11 +00:00
absolutePath = plugins.path.resolve(localPathArg);
} else {
absolutePath = localPathArg;
}
return absolutePath;
};
2016-03-12 07:24:05 +00:00
2024-04-12 12:59:49 +00:00
/*
* like makeAbsolute, but takes different complex contructs like arrays and objects
*/
2023-07-11 22:39:53 +00:00
export const toAbsolute = (relativeArg: string | string[], baseArg?: string): string | string[] => {
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'
);
2022-03-16 10:35:25 +00:00
return null;
2018-07-21 21:26:11 +00:00
}
};