From dd7149b1eda02c9018661b07618e5f5207f16304 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Fri, 12 Jul 2024 09:29:28 -0600 Subject: [PATCH] update scripts --- .../scripts/bitcoin/transaction_count.js | 3 +- scripts/add_metadata_to_dfx_json.js | 6 +- .../class_migration/add_example_dependency.js | 6 +- .../change_to_custom_candid_gen.js | 6 +- scripts/class_migration/clean_package.js | 6 +- .../copy_generated_candid_from_example.js | 6 +- scripts/class_migration/delete_tests_file.js | 6 +- scripts/class_migration/do_all_packages.js | 11 ++- scripts/class_migration/modify_pretest.js | 6 +- scripts/class_migration/name_package.js | 6 +- scripts/class_migration/update_test.js | 6 +- scripts/class_migration/update_ts_config.js | 6 +- scripts/class_migration/update_typescript.js | 9 +-- scripts/file_generator/bin.js | 12 +-- scripts/hash_file/bin.js | 12 +-- scripts/reorder_dfx_json.js | 6 +- scripts/simplify_tsconfig.js | 6 +- scripts/tsx_migration/ts_node_to_tsx.js | 76 +++++++++++++++++++ scripts/update_dfx_json_main.js | 6 +- 19 files changed, 115 insertions(+), 86 deletions(-) mode change 100644 => 100755 scripts/class_migration/do_all_packages.js create mode 100644 scripts/tsx_migration/ts_node_to_tsx.js diff --git a/examples/bitcoin_psbt/scripts/bitcoin/transaction_count.js b/examples/bitcoin_psbt/scripts/bitcoin/transaction_count.js index 63c8774d2b..c70c2b7a64 100644 --- a/examples/bitcoin_psbt/scripts/bitcoin/transaction_count.js +++ b/examples/bitcoin_psbt/scripts/bitcoin/transaction_count.js @@ -1,6 +1,5 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { execSync } = require('child_process'); +import { execSync } from 'child_process'; const CLI = '.bitcoin/bin/bitcoin-cli -conf=$(pwd)/.bitcoin.conf'; diff --git a/scripts/add_metadata_to_dfx_json.js b/scripts/add_metadata_to_dfx_json.js index c57db99d40..161944ac51 100644 --- a/scripts/add_metadata_to_dfx_json.js +++ b/scripts/add_metadata_to_dfx_json.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; const targetDirectory = process.argv[2] || '.'; // Use the provided directory or default to the current directory diff --git a/scripts/class_migration/add_example_dependency.js b/scripts/class_migration/add_example_dependency.js index 40e9681499..938fba84ad 100644 --- a/scripts/class_migration/add_example_dependency.js +++ b/scripts/class_migration/add_example_dependency.js @@ -1,7 +1,5 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Get the current directory name const currentDirectory = path.basename(process.cwd()); diff --git a/scripts/class_migration/change_to_custom_candid_gen.js b/scripts/class_migration/change_to_custom_candid_gen.js index 6ad5c038e0..6ae6d69dde 100644 --- a/scripts/class_migration/change_to_custom_candid_gen.js +++ b/scripts/class_migration/change_to_custom_candid_gen.js @@ -1,7 +1,5 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Path to the dfx.json file const dfxJsonPath = path.join(process.cwd(), 'dfx.json'); diff --git a/scripts/class_migration/clean_package.js b/scripts/class_migration/clean_package.js index 36ef773b86..3141c03213 100644 --- a/scripts/class_migration/clean_package.js +++ b/scripts/class_migration/clean_package.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Directories to be removed const directoriesToRemove = ['node_modules', '.azle', '.dfx']; diff --git a/scripts/class_migration/copy_generated_candid_from_example.js b/scripts/class_migration/copy_generated_candid_from_example.js index a33053aab8..2c5cf31cdc 100644 --- a/scripts/class_migration/copy_generated_candid_from_example.js +++ b/scripts/class_migration/copy_generated_candid_from_example.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Helper function to copy a file from src to dest const copyFile = (src, dest) => { diff --git a/scripts/class_migration/delete_tests_file.js b/scripts/class_migration/delete_tests_file.js index 5ff787b371..7dfa293f25 100644 --- a/scripts/class_migration/delete_tests_file.js +++ b/scripts/class_migration/delete_tests_file.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Path to the tests.ts file const testsFilePath = path.join(process.cwd(), 'test', 'tests.ts'); diff --git a/scripts/class_migration/do_all_packages.js b/scripts/class_migration/do_all_packages.js old mode 100644 new mode 100755 index 628371a31a..3b333303eb --- a/scripts/class_migration/do_all_packages.js +++ b/scripts/class_migration/do_all_packages.js @@ -1,9 +1,8 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { exec } = require('child_process'); +#!/usr/bin/env node + +import { exec } from 'child_process'; +import * as fs from 'fs'; +import * as path from 'path'; // Get the current directory name const currentDirectory = process.cwd(); diff --git a/scripts/class_migration/modify_pretest.js b/scripts/class_migration/modify_pretest.js index ae2fdd2fe4..9164d69396 100644 --- a/scripts/class_migration/modify_pretest.js +++ b/scripts/class_migration/modify_pretest.js @@ -1,7 +1,5 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Path to the pretest.ts file const pretestFilePath = path.join(process.cwd(), 'test', 'pretest.ts'); diff --git a/scripts/class_migration/name_package.js b/scripts/class_migration/name_package.js index 165263306a..079f0ea294 100644 --- a/scripts/class_migration/name_package.js +++ b/scripts/class_migration/name_package.js @@ -1,7 +1,5 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Get the current directory name const currentDirectory = path.basename(process.cwd()); diff --git a/scripts/class_migration/update_test.js b/scripts/class_migration/update_test.js index 1f7c4903fd..62b0e3cac3 100644 --- a/scripts/class_migration/update_test.js +++ b/scripts/class_migration/update_test.js @@ -1,7 +1,5 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Get the current directory name const currentDirectory = path.basename(process.cwd()); diff --git a/scripts/class_migration/update_ts_config.js b/scripts/class_migration/update_ts_config.js index 274f7a8810..6f82a2c6b6 100644 --- a/scripts/class_migration/update_ts_config.js +++ b/scripts/class_migration/update_ts_config.js @@ -1,7 +1,5 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Path to the tsconfig.json file const tsconfigPath = path.join(process.cwd(), 'tsconfig.json'); diff --git a/scripts/class_migration/update_typescript.js b/scripts/class_migration/update_typescript.js index f96bc28999..73fa6cad87 100644 --- a/scripts/class_migration/update_typescript.js +++ b/scripts/class_migration/update_typescript.js @@ -1,9 +1,6 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { exec } = require('child_process'); +import { exec } from 'child_process'; +import * as fs from 'fs'; +import * as path from 'path'; const tsVersion = '^5.2.2'; diff --git a/scripts/file_generator/bin.js b/scripts/file_generator/bin.js index 8c893f67a9..db2eb7f93a 100755 --- a/scripts/file_generator/bin.js +++ b/scripts/file_generator/bin.js @@ -1,12 +1,4 @@ #!/usr/bin/env node -// eslint-disable-next-line @typescript-eslint/no-var-requires -require('ts-node').register({ - transpileOnly: true, - ignore: [`node_modules/(?!azle)`], - compilerOptions: { - module: 'commonjs', - allowJs: true - } -}); -require('./main.ts'); +import 'tsx'; +import('./main.ts'); diff --git a/scripts/hash_file/bin.js b/scripts/hash_file/bin.js index 8c893f67a9..db2eb7f93a 100755 --- a/scripts/hash_file/bin.js +++ b/scripts/hash_file/bin.js @@ -1,12 +1,4 @@ #!/usr/bin/env node -// eslint-disable-next-line @typescript-eslint/no-var-requires -require('ts-node').register({ - transpileOnly: true, - ignore: [`node_modules/(?!azle)`], - compilerOptions: { - module: 'commonjs', - allowJs: true - } -}); -require('./main.ts'); +import 'tsx'; +import('./main.ts'); diff --git a/scripts/reorder_dfx_json.js b/scripts/reorder_dfx_json.js index 6f0e0fb54c..4d831a2ac8 100644 --- a/scripts/reorder_dfx_json.js +++ b/scripts/reorder_dfx_json.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs-extra'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs-extra'; +import * as path from 'path'; // Helper function to reorder keys in an object. const reorderKeys = (obj, order) => { diff --git a/scripts/simplify_tsconfig.js b/scripts/simplify_tsconfig.js index b1f2c8485b..caaba3d852 100644 --- a/scripts/simplify_tsconfig.js +++ b/scripts/simplify_tsconfig.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs-extra'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs-extra'; +import * as path from 'path'; // Read directory. const dirPath = './examples/motoko_examples'; // Replace with the path to the directory containing folders with tsconfig.json files diff --git a/scripts/tsx_migration/ts_node_to_tsx.js b/scripts/tsx_migration/ts_node_to_tsx.js new file mode 100644 index 0000000000..04a11ba2e7 --- /dev/null +++ b/scripts/tsx_migration/ts_node_to_tsx.js @@ -0,0 +1,76 @@ +/* eslint-disable @typescript-eslint/explicit-function-return-type */ + +import * as fs from 'fs'; +import * as path from 'path'; + +// Path to the package.json file +const packageJsonPath = path.join(process.cwd(), 'package.json'); + +// Helper function to sort an object by its keys +const sortObjectByKeys = (obj) => { + return Object.keys(obj) + .sort() + .reduce((result, key) => { + result[key] = obj[key]; + return result; + }, {}); +}; + +// Read and parse package.json +fs.readFile(packageJsonPath, 'utf8', (err, data) => { + if (err) { + console.error(`Error reading package.json file: ${err}`); + return; + } + + let packageJson; + try { + packageJson = JSON.parse(data); + } catch (parseErr) { + console.error(`Error parsing package.json: ${parseErr}`); + return; + } + + const tsxVersion = '^4.15.7'; + let tsNodeRemoved = false; + + // Check and update dependencies + if (packageJson.dependencies && packageJson.dependencies['ts-node']) { + delete packageJson.dependencies['ts-node']; + packageJson.dependencies['tsx'] = tsxVersion; + packageJson.dependencies = sortObjectByKeys(packageJson.dependencies); + tsNodeRemoved = true; + } + + // Check and update devDependencies + if (packageJson.devDependencies && packageJson.devDependencies['ts-node']) { + delete packageJson.devDependencies['ts-node']; + packageJson.devDependencies['tsx'] = tsxVersion; + packageJson.devDependencies = sortObjectByKeys( + packageJson.devDependencies + ); + tsNodeRemoved = true; + } + + if (!tsNodeRemoved) { + console.log('ts-node not found in dependencies or devDependencies.'); + return; + } + + // Write the updated package.json back to the file + fs.writeFile( + packageJsonPath, + JSON.stringify(packageJson, null, 2), + 'utf8', + (writeErr) => { + if (writeErr) { + console.error( + `Error writing updated package.json: ${writeErr}` + ); + return; + } + + console.log('package.json successfully updated.'); + } + ); +}); diff --git a/scripts/update_dfx_json_main.js b/scripts/update_dfx_json_main.js index 07bf0b9e23..8ebbcbf751 100644 --- a/scripts/update_dfx_json_main.js +++ b/scripts/update_dfx_json_main.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); +import * as fs from 'fs'; +import * as path from 'path'; // Helper function to update the canister definition function updateCanister(canister) {