Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions template/plugins/compile-js/plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable no-console */
const { execSync, spawnSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');

const TYPESCRIPT_VERSION = '5.6.3';

Expand All @@ -9,17 +11,20 @@ function isYarnAvailable() {
return !!(
execSync('yarn --version', {
stdio: [0, 'pipe', 'ignore'],
shell: true,
}).toString() || ''
).trim();
} catch {
return null;
}
}

function isNpmAvailable() {
try {
return !!(
execSync('npm --version', {
stdio: [0, 'pipe', 'ignore'],
shell: true,
}).toString() || ''
).trim();
} catch {
Expand All @@ -33,7 +38,6 @@ module.exports = {
let packageManager = null;
let addCmd = null;

// react-native cli prefer yarn so we follow the same logic
if (isYarnAvailable()) {
packageManager = 'yarn';
addCmd = 'add';
Expand All @@ -56,7 +60,7 @@ module.exports = {
const installTypeScriptCmd = spawnSync(
packageManager,
[addCmd, '-D', `typescript@${TYPESCRIPT_VERSION}`],
{ stdio: 'inherit' },
{ stdio: 'inherit', shell: true },
);
if (installTypeScriptCmd.error) {
console.error(installTypeScriptCmd.error);
Expand All @@ -67,7 +71,7 @@ module.exports = {
const transpileCmd = spawnSync(
'npx',
['tsc', '--project', `plugins/compile-js/tsconfig.build.json`],
{ stdio: 'inherit' },
{ stdio: 'inherit', shell: true },
);
if (transpileCmd.error) {
console.error(transpileCmd.error);
Expand All @@ -76,23 +80,25 @@ module.exports = {

try {
console.log('🖼️ Copying assets...');
execSync('cp -R src/theme/assets/images js/src/theme/assets/images');

fs.cpSync(
path.join('src', 'theme', 'assets', 'images'),
path.join('js', 'src', 'theme', 'assets', 'images'),
{ recursive: true }
);
console.log('♻️ Replacing source...');
execSync('rm -rf src', { stdio: 'pipe' });
execSync('cp -R js/src ./src', { stdio: 'pipe' });
execSync('rm -rf js', { stdio: 'pipe' });
fs.rmSync('src', { recursive: true, force: true });
fs.cpSync(path.join('js', 'src'), 'src', { recursive: true });
fs.rmSync('js', { recursive: true, force: true });
} catch {
console.error(
'🚨 Failed to copy assets or replace source. If you are using windows, please use git bash.',
'🚨 Failed to copy assets or replace source.',
);
process.exit(1);
}

console.log('🌀 Removing types ...');
execSync('rm -rf src/theme/types', { stdio: 'pipe' });
execSync('rm -f src/navigation/paths.js', { stdio: 'pipe' });
execSync('rm -f src/navigation/types.js', { stdio: 'pipe' });
fs.rmSync(path.join('src', 'theme', 'types'), { recursive: true, force: true });
fs.rmSync(path.join('src', 'navigation', 'paths.js'), { force: true });
fs.rmSync(path.join('src', 'navigation', 'types.js'), { force: true });
}

resolve();
Expand Down