From 36f3d09c0e3e7a2d8290480a65bc57f2f4353722 Mon Sep 17 00:00:00 2001 From: Terone Date: Fri, 11 Aug 2023 11:22:25 +0700 Subject: [PATCH] [#121] Update vite initialization to folder for easier updates --- .../src/template/initialize-vite-app.ts | 25 ++++++++++++++----- .../test/commands/generate/index.test.ts | 6 ++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/cli-tool/src/template/initialize-vite-app.ts b/packages/cli-tool/src/template/initialize-vite-app.ts index c3f6662..e1b08cc 100644 --- a/packages/cli-tool/src/template/initialize-vite-app.ts +++ b/packages/cli-tool/src/template/initialize-vite-app.ts @@ -10,8 +10,8 @@ const replaceAppNameInFiles = ['package.json', 'index.html']; type InitViteOptions = { dest: string; + templateReference: string; // Git branch name or path to a local template folder appName: string; - branch: string; }; const downloadTemplateRepository = ( @@ -23,7 +23,7 @@ const downloadTemplateRepository = ( { gitHubAccount: TEMPLATE_OWNER, repositoryName: TEMPLATE_REPO, - branch: options.branch, + branch: options.templateReference, }, options.appName, options.dest, @@ -40,9 +40,20 @@ const extractViteTemplateFolder = (options: InitViteOptions): Promise => { ); }; +const copyTemplateFiles = (options: InitViteOptions): Promise => { + CliUx.ux.info('Copying template source files...'); + const branchPath = options.templateReference.replace('/', '-'); + + return runCommand( + 'cp', + ['-r', `${TEMPLATE_REPO}-${branchPath}/vite-template/`, options.appName], + options.dest, + ); +}; + const renameFolder = (options: InitViteOptions): Promise => { CliUx.ux.info('Rename your app folder...'); - const branchPath = options.branch.replace('/', '-'); + const branchPath = options.templateReference.replace('/', '-'); return runCommand( 'mv', @@ -69,7 +80,7 @@ const npmInstall = (options: InitViteOptions): Promise => { const cleanTemporaryFiles = (options: InitViteOptions): Promise => { CliUx.ux.info('Remove zip and unwanted files...'); - const branchPath = options.branch.replace('/', '-'); + const branchPath = options.templateReference.replace('/', '-'); // Remove the archive return runCommand('rm', [`${options.appName}.gz`], options.dest).then(() => { @@ -83,8 +94,10 @@ const cleanTemporaryFiles = (options: InitViteOptions): Promise => { }; const initializeViteApp = async(options: InitViteOptions): Promise => { - return downloadTemplateRepository(options) - .then(() => extractViteTemplateFolder(options)) + // If given a branch name, use + // return downloadTemplateRepository(options) + // .then(()=> extractViteTemplateFolder(options)) + return copyTemplateFiles(options) .then(() => renameFolder(options)) .then(() => replaceAppName(options)) .then(() => npmInstall(options)) diff --git a/packages/cli-tool/test/commands/generate/index.test.ts b/packages/cli-tool/test/commands/generate/index.test.ts index b3d3bba..8b130b6 100644 --- a/packages/cli-tool/test/commands/generate/index.test.ts +++ b/packages/cli-tool/test/commands/generate/index.test.ts @@ -8,10 +8,8 @@ import { tailwindCssTestData } from '../../add-ons/ui-framework/tailwind-css'; import { gitHubTestData, gitLabTestData, noVersionControlTestData } from '../../add-ons/version-control'; import { TestScenario } from '../../helpers/test-scenario'; -const craTemplateReference = `file:./react-templates/packages/cra-template`; -// TODO: Adjust viteTemplateReference to use commit hash of development branch for vite template -// https://github.com/nimblehq/react-templates/commit/52288d1e5e560bcc717f760f1fd19f7cb1b0085e -const viteTemplateReference = '52288d1e5e560bcc717f760f1fd19f7cb1b0085e'; +const craTemplateReference = 'file:./react-templates/packages/cra-template'; +const viteTemplateReference = 'file:./react-templates/packages/vite-template'; const projectName = 'test-app'; const testFolderPath = '../../../';