2021-11-30 17:30:25 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-07-10 22:10:48 +02:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import path from 'node:path';
|
|
|
|
import yargs from 'yargs';
|
|
|
|
import { hideBin } from 'yargs/helpers';
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:10:48 +02:00
|
|
|
import { destinationDirectory, distDirectory, foundryconfigFile, name } from './const.js';
|
2021-11-30 17:30:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the data path of Foundry VTT based on what is configured in the {@link foundryconfigFile}.
|
|
|
|
*/
|
|
|
|
function getDataPath() {
|
2023-07-10 22:10:48 +02:00
|
|
|
const config = fs.readJSONSync(foundryconfigFile);
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:10:48 +02:00
|
|
|
if (config?.dataPath) {
|
|
|
|
if (!fs.existsSync(path.resolve(config.dataPath))) {
|
|
|
|
throw new Error('User data path invalid, no Data directory found');
|
2021-11-30 17:30:25 +01:00
|
|
|
}
|
2023-07-10 22:10:48 +02:00
|
|
|
return path.resolve(config.dataPath);
|
|
|
|
} else {
|
|
|
|
throw new Error(`No user data path defined in ${foundryconfigFile}`);
|
|
|
|
}
|
2021-11-30 17:30:25 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Link the built package to the user data folder.
|
|
|
|
* @param {boolean} clean Whether to remove the link instead of creating it
|
|
|
|
*/
|
|
|
|
async function linkPackage(clean) {
|
2023-07-10 22:10:48 +02:00
|
|
|
if (!fs.existsSync(path.resolve('system.json'))) {
|
|
|
|
throw new Error('Could not find system.json');
|
|
|
|
}
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:10:48 +02:00
|
|
|
const linkDirectory = path.resolve(getDataPath(), 'Data', destinationDirectory, name);
|
2021-11-30 17:30:25 +01:00
|
|
|
|
2023-07-10 22:10:48 +02:00
|
|
|
if (clean) {
|
|
|
|
console.log(`Removing link to built package at ${linkDirectory}.`);
|
|
|
|
await fs.remove(linkDirectory);
|
|
|
|
} else if (!fs.existsSync(linkDirectory)) {
|
|
|
|
console.log(`Linking built package to ${linkDirectory}.`);
|
|
|
|
await fs.ensureDir(path.resolve(linkDirectory, '..'));
|
|
|
|
await fs.symlink(path.resolve('.', distDirectory), linkDirectory);
|
|
|
|
}
|
2021-11-30 17:30:25 +01:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:10:48 +02:00
|
|
|
const argv = yargs(hideBin(process.argv)).usage('Usage: $0').option('clean', {
|
|
|
|
alias: 'c',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
description: 'Remove the link instead of creating it',
|
2021-11-30 17:30:25 +01:00
|
|
|
}).argv;
|
|
|
|
const clean = argv.c;
|
|
|
|
await linkPackage(clean);
|