@@ -4,7 +4,9 @@ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
44import { ProjectConfiguration , workspaceRoot } from '@nx/devkit' ;
55
66describe ( 'nx-github-pages' , ( ) => {
7- let projectDirectory , remote , remoteDirectory : string ;
7+ let projectDirectory : string ;
8+ let remote : string ;
9+ let remoteDirectory : string ;
810
911 // The state of the remote matters for some tests so we
1012 // are going to reinit everything everytime. This is not the most
@@ -26,15 +28,21 @@ describe('nx-github-pages', () => {
2628 } ) ;
2729
2830 it ( 'should deploy to gh-pages branch of remote' , ( ) => {
29- generateReactApp ( projectDirectory , 'my-app' ) ;
31+ const appName = 'my-app' ;
32+ const nxProjectName = getNxProjectName ( projectDirectory , appName ) ;
33+ generateReactApp ( projectDirectory , appName ) ;
3034
3135 runCommand (
32- ' npx nx g nx-github-pages:configuration --project my-app --user.name deployment-bot --user.email deployment@testing.com --no-interactive' ,
36+ ` npx nx g nx-github-pages:configuration --project ${ nxProjectName } --user.name deployment-bot --user.email deployment@testing.com --no-interactive` ,
3337 projectDirectory ,
3438 { }
3539 ) ;
3640
37- runCommand ( 'npx nx deploy my-app --no-interactive' , projectDirectory , { } ) ;
41+ runCommand (
42+ `npx nx deploy ${ nxProjectName } --no-interactive` ,
43+ projectDirectory ,
44+ { }
45+ ) ;
3846
3947 // Check that the gh-pages branch exists in the remote
4048 runCommand ( 'git checkout gh-pages' , remoteDirectory , { } ) ;
@@ -46,32 +54,66 @@ describe('nx-github-pages', () => {
4654 } ) ;
4755
4856 it ( 'should sync via merge sync enabled and deployment already exists' , ( ) => {
57+ const appName = 'my-app' ;
58+ const nxProjectName = getNxProjectName ( projectDirectory , appName ) ;
59+
4960 // SETUP
5061 // Create a new app
51- generateReactApp ( projectDirectory , 'my-app' ) ;
62+ generateReactApp ( projectDirectory , appName ) ;
5263 // Create the configuration
5364 runCommand (
54- ' npx nx g nx-github-pages:configuration --user.name deployment-bot --user.email deployment@testing.com --project my-app --no-interactive' ,
65+ ` npx nx g nx-github-pages:configuration --user.name deployment-bot --user.email deployment@testing.com --project ${ nxProjectName } --no-interactive` ,
5566 projectDirectory ,
5667 { }
5768 ) ;
5869 // Initial deployment
59- runCommand ( 'npx nx deploy my-app --no-interactive' , projectDirectory , { } ) ;
60- // enable sync settings
61- updateJsonFile < ProjectConfiguration > (
70+ runCommand (
71+ `npx nx deploy ${ nxProjectName } --no-interactive` ,
6272 projectDirectory ,
63- 'apps/my-app/project.json' ,
64- ( json ) => {
65- json . targets . deploy . options . syncWithBaseBranch = true ;
66- json . targets . deploy . options . syncGitOptions = [
67- '--allow-unrelated-histories' ,
68- '-s ours' ,
69- ] ;
70- return json ;
71- }
73+ { }
7274 ) ;
73- // Update the app
74- updateFile ( projectDirectory , 'apps/my-app/src/index.html' , ( content ) =>
75+ // enable sync settings — Nx 22+ stores project config in package.json under "nx"
76+ const projectConfigPath = existsSync (
77+ join ( projectDirectory , `apps/${ appName } /project.json` )
78+ )
79+ ? `apps/${ appName } /project.json`
80+ : `apps/${ appName } /package.json` ;
81+ const isPackageJson = projectConfigPath . endsWith ( 'package.json' ) ;
82+
83+ if ( isPackageJson ) {
84+ updateJsonFile < { nx : ProjectConfiguration } > (
85+ projectDirectory ,
86+ projectConfigPath ,
87+ ( json ) => {
88+ json . nx . targets . deploy . options . syncWithBaseBranch = true ;
89+ json . nx . targets . deploy . options . syncGitOptions = [
90+ '--allow-unrelated-histories' ,
91+ '-s ours' ,
92+ ] ;
93+ return json ;
94+ }
95+ ) ;
96+ } else {
97+ updateJsonFile < ProjectConfiguration > (
98+ projectDirectory ,
99+ projectConfigPath ,
100+ ( json ) => {
101+ json . targets . deploy . options . syncWithBaseBranch = true ;
102+ json . targets . deploy . options . syncGitOptions = [
103+ '--allow-unrelated-histories' ,
104+ '-s ours' ,
105+ ] ;
106+ return json ;
107+ }
108+ ) ;
109+ }
110+ // Update the app — Nx 22+ with Vite puts index.html at the app root
111+ const indexHtmlPath = existsSync (
112+ join ( projectDirectory , `apps/${ appName } /index.html` )
113+ )
114+ ? `apps/${ appName } /index.html`
115+ : `apps/${ appName } /src/index.html` ;
116+ updateFile ( projectDirectory , indexHtmlPath , ( content ) =>
75117 content . replace (
76118 '<div id="root"></div>' ,
77119 '<h1>Updated</h1><div id="root"></div>'
@@ -80,7 +122,11 @@ describe('nx-github-pages', () => {
80122
81123 // TEST
82124 // Deploy the updated app
83- runCommand ( 'npx nx deploy my-app --no-interactive' , projectDirectory , { } ) ;
125+ runCommand (
126+ `npx nx deploy ${ nxProjectName } --no-interactive` ,
127+ projectDirectory ,
128+ { }
129+ ) ;
84130
85131 // ASSERT
86132 // Check that the gh-pages branch exists in the remote
@@ -112,7 +158,7 @@ function createTestProject(remote: string) {
112158 } ) ;
113159
114160 execSync (
115- `npx --yes create-nx-workspace@latest ${ projectName } --preset apps --nxCloud=skip --no-interactive` ,
161+ `npx --yes create-nx-workspace@latest ${ projectName } --preset apps --nxCloud=skip --skipGit -- no-interactive` ,
116162 {
117163 cwd : dirname ( projectDirectory ) ,
118164 stdio : 'inherit' ,
@@ -217,6 +263,17 @@ function updateJsonFile<T>(
217263 } ) ;
218264}
219265
266+ function getNxProjectName (
267+ projectDirectory : string ,
268+ appName : string
269+ ) : string {
270+ const pkgJson = JSON . parse (
271+ readFileSync ( join ( projectDirectory , 'package.json' ) , 'utf-8' )
272+ ) ;
273+ const scopeMatch = ( pkgJson . name as string ) ?. match ( / ^ ( @ [ ^ / ] + ) \/ / ) ;
274+ return scopeMatch ? `${ scopeMatch [ 1 ] } /${ appName } ` : appName ;
275+ }
276+
220277function generateReactApp ( projectDirectory : string , projectName : string ) {
221278 runCommand (
222279 `npx nx g @nx/react:app ${ projectName } --directory apps/${ projectName } --projectNameAndRootFormat=as-provided --e2eTestRunner none --unitTestRunner none --linter none --no-interactive` ,
0 commit comments