Skip to content

Commit

Permalink
feat: add mono link ui script
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Aug 26, 2024
1 parent 29f2174 commit 717737c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"chromatic": "npx chromatic --project-token=CHROMATIC_PROJECT_TOKEN",
"preinstall": "npx only-allow pnpm",
"mono:link": "node scripts/mono-link.js && pnpm install",
"mono:unlink": "node scripts/mono-unlink.js && pnpm install"
"mono:unlink": "node scripts/mono-unlink.js && pnpm install",
"mono:link:ui": "node scripts/mono-link-ui.js && pnpm install"
},
"license": "MIT",
"main": "index.js",
Expand Down
53 changes: 53 additions & 0 deletions scripts/mono-link-ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import fs from 'fs/promises';

const filePath = './package.json';
const backupFilePath = './package-original.json';

async function modifyUiPackageJson() {
try {
// Check if a backup already exists
try {
await fs.access(backupFilePath);
console.log(`Backup file already exists: ${backupFilePath}`);
} catch {
await fs.copyFile(filePath, backupFilePath);
console.log(`Backup created: ${backupFilePath}`);
}

// Read and parse the package.json file
const data = await fs.readFile(filePath, 'utf-8');
let packageJson = JSON.parse(data);

// Modify the @leather.io/ui dependency
packageJson.dependencies['@leather.io/ui'] = 'file:../mono/packages/ui';

// Ensure pnpm and pnpm.overrides are initialized
if (!packageJson.pnpm) {
packageJson.pnpm = {};
}
if (!packageJson.pnpm.overrides) {
packageJson.pnpm.overrides = {};
}

// Add the specified overrides
const overrides = {
'@leather.io/rpc': 'file:../mono/packages/rpc',
'@leather.io/constants': 'file:../mono/packages/constants',
'@leather.io/models': 'file:../mono/packages/models',
'@leather.io/tokens': 'file:../mono/packages/tokens',
'@leather.io/utils': 'file:../mono/packages/utils'
};

packageJson.pnpm.overrides = { ...packageJson.pnpm.overrides, ...overrides };

// Write the modified package.json back to the file
await fs.writeFile(filePath, JSON.stringify(packageJson, null, 2) + '\n', 'utf-8');
console.log(`Successfully updated ${filePath}`);

} catch (err) {
console.error(`Error: ${err}`);
}
}

// Execute the function
modifyUiPackageJson();
20 changes: 3 additions & 17 deletions scripts/mono-link.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
import fs from 'fs/promises';

// Paths to your package.json files
const filePath = './package.json';
const backupFilePath = './package-original.json';

async function modifyPackageJson() {
try {
// Check if backup file exists
try {
await fs.access(backupFilePath);
console.log(`Backup file already exists: ${backupFilePath}`);
} catch {
// Backup the original package.json
await fs.copyFile(filePath, backupFilePath);
console.log(`Backup created: ${backupFilePath}`);
}

// Read the package.json file
const data = await fs.readFile(filePath, 'utf-8');

// Parse the JSON
let packageJson = JSON.parse(data);

// Update dependencies to use relative paths
const relativePaths = [
'@leather.io/bitcoin',
'@leather.io/constants',
'@leather.io/crypto',
'@leather.io/models',
'@leather.io/query',
'@leather.io/tokens',
'@leather.io/ui',
'@leather.io/utils',
'@leather.io/stacks',
];

const devRelativePaths = [
'@leather.io/panda-preset',
'@leather.io/rpc'
];

const devRelativePaths = ['@leather.io/panda-preset', '@leather.io/rpc'];

relativePaths.forEach(dep => {
if (packageJson.dependencies[dep]) {
Expand All @@ -52,24 +41,21 @@ async function modifyPackageJson() {
}
});

// Update pnpm.overrides to use relative paths
if (!packageJson.pnpm) {
packageJson.pnpm = {};
}
if (!packageJson.pnpm.overrides) {
packageJson.pnpm.overrides = {};
}

relativePaths.concat(devRelativePaths).forEach(dep => {
[...relativePaths, ...devRelativePaths].forEach(dep => {
if (packageJson.dependencies[dep] || packageJson.devDependencies[dep]) {
packageJson.pnpm.overrides[dep] = `file:../mono/packages/${dep.split('/').pop()}`;
}
});

// Write the modified JSON back to the file with an empty line at the end
await fs.writeFile(filePath, JSON.stringify(packageJson, null, 2) + '\n', 'utf-8');
console.log(`Successfully updated ${filePath}`);

} catch (err) {
console.error(`Error: ${err}`);
}
Expand Down
4 changes: 0 additions & 4 deletions scripts/mono-unlink.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import fs from 'fs/promises';

// Paths to your package.json files
const filePath = './package.json';
const backupFilePath = './package-original.json';

async function revertPackageJson() {
try {
// Check if backup file exists
try {
await fs.access(backupFilePath);
} catch {
throw new Error(`Backup file not found: ${backupFilePath}`);
}

// Restore the original package.json
await fs.copyFile(backupFilePath, filePath);
console.log(`Successfully reverted ${filePath}`);

// Remove the backup file
await fs.unlink(backupFilePath);
console.log(`Backup file removed: ${backupFilePath}`);

Expand Down

0 comments on commit 717737c

Please sign in to comment.