Skip to content

Commit aa4fcab

Browse files
committed
fix: test fixes
1 parent ce09e07 commit aa4fcab

File tree

7 files changed

+272
-863
lines changed

7 files changed

+272
-863
lines changed

packages/gluestack-cli/src/dependencies.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const projectBasedDependencies: Dependency = {
1717
autoprefixer: 'latest',
1818
'react-native-web': 'latest',
1919
'@gluestack/ui-next-adapter': 'latest',
20+
'@types/react-native': '^0.73.0',
2021
},
2122
},
2223
expo: {
@@ -42,6 +43,7 @@ const dependenciesConfig: Dependencies = {
4243
},
4344
devDependencies: {
4445
jscodeshift: '0.15.2',
46+
prettier: '^3.3.2',
4547
},
4648
},
4749
accordion: {

packages/gluestack-cli/src/util/config-generate/index.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,26 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import { config } from '../../config';
44
import { projectRootPath } from '..';
5-
import { cosmiconfig } from 'cosmiconfig';
65
import fg from 'fast-glob';
7-
import {
8-
RawConfig,
9-
RawConfigSchema,
10-
PROJECT_SHARED_IGNORE,
11-
} from './config-types';
6+
import { RawConfig, PROJECT_SHARED_IGNORE } from './config-types';
127

138
const fileExtensions = ['.tsx', '.jsx', '.ts', '.js'];
149
const possibleIndexFiles = ['_app', 'index', 'App'];
1510
const possibleDirectories = ['src', 'pages', 'app', 'components'];
1611

17-
const explorer = cosmiconfig('gluestack-ui', {
18-
searchPlaces: ['gluestack-ui.json'],
19-
});
12+
function findDirectory(rootDir: string, relativePaths: string[]) {
13+
for (const relPath of relativePaths) {
14+
const dirPath = path.join(rootDir, relPath);
15+
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
16+
return dirPath.replace(`${rootDir}/`, '');
17+
}
18+
}
19+
return '';
20+
}
2021

2122
async function checkIfInitialized(cwd: string): Promise<boolean> {
2223
try {
23-
const initializeStatus = await fg.glob('gluestack-ui.config.json', {
24-
cwd,
25-
deep: 8,
26-
ignore: PROJECT_SHARED_IGNORE,
27-
});
24+
const initializeStatus = await getComponentsPath();
2825
if (initializeStatus.length) {
2926
return true;
3027
}
@@ -55,18 +52,6 @@ async function getComponentsPath(): Promise<string> {
5552
return resolvedComponentsPath;
5653
}
5754

58-
async function checkConfigSchema(cwd: string): Promise<RawConfig | null> {
59-
try {
60-
const configResult = await explorer.search(cwd);
61-
if (!configResult) {
62-
return null;
63-
}
64-
return RawConfigSchema.parse(configResult.config);
65-
} catch (error) {
66-
throw new Error(`Invalid configuration found in ${cwd}/gluestack-ui.json.`);
67-
}
68-
}
69-
7055
function getEntryPathAndComponentsPath(): {
7156
entryPath: string[];
7257
} {
@@ -119,9 +104,14 @@ async function generateConfig(resultConfig: RawConfig) {
119104
}
120105

121106
async function generateGluestackConfig() {
107+
const componentPath = path.resolve(
108+
projectRootPath,
109+
config.writableComponentsPath
110+
);
111+
122112
const gluestackConfig = {
123113
app: {
124-
components: config.writableComponentsPath,
114+
components: componentPath.replace(`${projectRootPath}/`, ''),
125115
},
126116
};
127117
const targetPath = path.resolve(projectRootPath, 'gluestack-ui.config.json');
@@ -139,4 +129,5 @@ export {
139129
getConfigPath,
140130
getComponentsPath,
141131
generateGluestackConfig,
132+
findDirectory,
142133
};

packages/gluestack-cli/src/util/config-generate/next-config-helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
NextResolvedConfig,
77
PROJECT_SHARED_IGNORE,
88
} from './config-types';
9-
import { generateConfig, getConfigPath } from '.';
9+
import { findDirectory, generateConfig, getConfigPath } from '.';
1010
import { config } from '../../config';
11-
import { findDirectory, projectRootPath } from '..';
11+
import { projectRootPath } from '..';
1212

1313
//next project type initialization
1414
async function getNextProjectType(cwd: string): Promise<string | undefined> {

packages/gluestack-cli/src/util/index.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os from 'os';
2-
import path, { join, dirname, extname } from 'path';
2+
import { join, dirname, extname, resolve } from 'path';
33
import fs, { stat } from 'fs-extra';
44
import {
55
log,
@@ -14,6 +14,7 @@ import finder from 'find-package-json';
1414
import simpleGit from 'simple-git';
1515
import { spawnSync } from 'child_process';
1616
import { config } from '../config';
17+
import prettier from 'prettier';
1718
import { dependenciesConfig, projectBasedDependencies } from '../dependencies';
1819

1920
// const stat = util.promisify(fs.stat);
@@ -538,14 +539,29 @@ const checkIfFolderExists = async (path: string): Promise<boolean> => {
538539
}
539540
};
540541

541-
function findDirectory(rootDir: string, relativePaths: string[]) {
542-
for (const relPath of relativePaths) {
543-
const dirPath = path.join(rootDir, relPath);
544-
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
545-
return dirPath.replace(`${rootDir}/`, '');
542+
async function formatFileWithPrettier(filePath: string | undefined) {
543+
try {
544+
if (!filePath) {
545+
log.error(
546+
`\x1b[31mError formatting file: Invalid file path provided\x1b[0m`
547+
);
548+
return;
546549
}
550+
const fileContent = fs.readFileSync(filePath, 'utf8');
551+
552+
// Get Prettier configuration (if any) from the project
553+
const prettierConfig = await prettier.resolveConfig(filePath);
554+
555+
// Format the file content using Prettier
556+
const formattedContent = await prettier.format(fileContent, {
557+
...prettierConfig,
558+
filepath: filePath, // This ensures Prettier uses the correct parser based on the file extension
559+
});
560+
561+
fs.writeFileSync(filePath, formattedContent, 'utf8');
562+
} catch (err) {
563+
log.error(`\x1b[Error formatting file : ${(err as Error).message}\x1b[0m`);
547564
}
548-
return '';
549565
}
550566

551567
export {
@@ -558,6 +574,6 @@ export {
558574
checkWritablePath,
559575
addDependencies,
560576
getExistingComponentStyle,
561-
findDirectory,
577+
formatFileWithPrettier,
562578
projectRootPath,
563579
};

packages/gluestack-cli/src/util/init-gluestack/index.ts

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
getAdditionalDependencies,
2424
addDependencies,
2525
projectRootPath,
26+
formatFileWithPrettier,
2627
} from '..';
2728

2829
const _currDir = process.cwd();
@@ -61,13 +62,17 @@ const InitializeGlueStack = async ({
6162
projectType,
6263
config.style
6364
);
64-
await generateProjectConfigAndInit(projectType, confirmOverride);
65+
const resolvedConfig = await generateProjectConfigAndInit(
66+
projectType,
67+
confirmOverride
68+
);
6569
await addProvider();
6670
await addDependencies(
6771
installationMethod,
6872
inputComponent,
6973
additionalDependencies
7074
);
75+
await formatFileWithPrettier(resolvedConfig?.app.entry);
7176
log.step(
7277
'Please refer the above link for more details --> \x1b[33mhttps://gluestack.io/ui/nativewind/docs/overview/introduction \x1b[0m'
7378
);
@@ -111,7 +116,10 @@ async function addProvider() {
111116
}
112117
}
113118

114-
async function updateTailwindConfig(resolvedConfig: RawConfig) {
119+
async function updateTailwindConfig(
120+
resolvedConfig: RawConfig,
121+
projectType: string
122+
) {
115123
try {
116124
const tailwindConfigRootPath = join(
117125
_homeDir,
@@ -128,7 +136,7 @@ async function updateTailwindConfig(resolvedConfig: RawConfig) {
128136
`${config.codeModesDir}/tailwind-config-transform.ts`
129137
);
130138
exec(
131-
`npx jscodeshift -t ${transformerPath} ${tailwindConfigPath} --paths='${allNewPaths}'`
139+
`npx jscodeshift -t ${transformerPath} ${tailwindConfigPath} --paths='${allNewPaths}' --projectType='${projectType}' `
132140
);
133141
} catch (err) {
134142
log.error(`\x1b[31mError: ${err as Error}\x1b[0m`);
@@ -201,7 +209,11 @@ async function updateGlobalCss(resolvedConfig: RawConfig): Promise<void> {
201209
}
202210
}
203211

204-
async function commonInitialization(projectType: string, resolvedConfig: any) {
212+
async function commonInitialization(
213+
projectType: string,
214+
resolvedConfig: any,
215+
permission: boolean | symbol
216+
) {
205217
try {
206218
const resolvedConfigValues = Object.values(resolvedConfig).flat(Infinity);
207219
const flattenedConfigValues = resolvedConfigValues.flatMap((value) =>
@@ -231,10 +243,32 @@ async function commonInitialization(projectType: string, resolvedConfig: any) {
231243
join(projectRootPath, 'nativewind-env.d.ts')
232244
);
233245

234-
//add or update all the above files
235-
await updateTailwindConfig(resolvedConfig);
236-
await updateTSConfig(projectType);
237-
await updateGlobalCss(resolvedConfig);
246+
permission && (await updateTSConfig(projectType));
247+
permission && (await updateGlobalCss(resolvedConfig));
248+
await updateTailwindConfig(resolvedConfig, projectType);
249+
250+
//function to update package.json script to implement darkMode in expo, will be removed later
251+
if (projectType === config.expoProject) {
252+
const packageJsonPath = join(projectRootPath, 'package.json');
253+
const packageJson = JSON.parse(
254+
await fs.readFile(packageJsonPath, 'utf8')
255+
);
256+
const devices = ['android', 'ios', 'web'];
257+
258+
//get existing value of scripts
259+
260+
devices.forEach((device) => {
261+
const script = packageJson.scripts[device];
262+
// packageJson.scripts[device] = `DARK_MODE=media expo start --${device}`;
263+
packageJson.scripts[device] = `DARK_MODE=media `.concat(script);
264+
});
265+
266+
await fs.writeFile(
267+
packageJsonPath,
268+
JSON.stringify(packageJson, null, 2),
269+
'utf8'
270+
);
271+
}
238272
} catch (err) {
239273
log.error(`\x1b[31mError: ${err as Error}\x1b[0m`);
240274
}
@@ -383,30 +417,35 @@ async function generateProjectConfigAndInit(
383417
projectType: string,
384418
confirmOverride: boolean | symbol
385419
) {
420+
let permission;
421+
if (confirmOverride === false || typeof confirmOverride === 'symbol') {
422+
permission = false;
423+
} else permission = true;
386424
let resolvedConfig; // Initialize with a default value
387425
if (projectType !== 'library') {
388426
switch (projectType) {
389427
case config.nextJsProject:
390428
resolvedConfig = await generateConfigNextApp();
391-
confirmOverride && (await initNatiwindNextApp(resolvedConfig));
429+
permission && (await initNatiwindNextApp(resolvedConfig));
392430
break;
393431
case config.expoProject:
394432
resolvedConfig = await generateConfigExpoApp();
395-
confirmOverride && (await initNatiwindExpoApp(resolvedConfig));
433+
permission && (await initNatiwindExpoApp(resolvedConfig));
396434
break;
397435
case config.reactNativeCLIProject:
398436
resolvedConfig = await generateConfigRNApp();
399-
confirmOverride && (await initNatiwindRNApp(resolvedConfig));
437+
permission && (await initNatiwindRNApp(resolvedConfig));
400438
break;
401439
default:
402440
break;
403441
}
404442

405-
await commonInitialization(projectType, resolvedConfig);
443+
await commonInitialization(projectType, resolvedConfig, permission);
406444
} else {
407445
//write function to generate config for library
408446
await generateGluestackConfig();
409447
}
448+
return resolvedConfig;
410449
}
411450

412451
const filesToOverride = (projectType: string) => {
@@ -444,6 +483,9 @@ function getStringLengthWithoutAnsi(string: string) {
444483
}
445484

446485
async function overrideWarning(files: string[]) {
486+
if (files.length === 0) {
487+
return true;
488+
}
447489
const boxLength = 90;
448490
console.log(`\x1b[33m
449491
${'─'.repeat(boxLength)}
@@ -477,4 +519,4 @@ ${files
477519
return confirmInput;
478520
}
479521

480-
export { InitializeGlueStack, filesToOverride, overrideWarning };
522+
export { InitializeGlueStack };

packages/gluestack-cli/template/template-codemods/tailwind-config-transform.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const transform: Transform = (file, api, options) => {
4040
});
4141

4242
// if important: true roperty not found, create it
43-
if (!importantProperty.size()) {
43+
if (!importantProperty.size() && options.projectType === 'nextjs') {
4444
// Find the 'presets' property index
4545
let presetsIndex = -1;
4646
const properties = tailwindConfig.get(0).node.right.properties;
@@ -56,14 +56,35 @@ const transform: Transform = (file, api, options) => {
5656
const importantPropertyNode = j.property(
5757
'init',
5858
j.identifier('important'),
59-
j.literal(true)
59+
j.literal('html')
6060
);
6161

6262
// Insert the important property node after presets
6363
properties.splice(presetsIndex + 1, 0, importantPropertyNode);
6464
}
6565
}
6666

67+
// Find and modify the 'darkMode' property
68+
const darkModeProperty = tailwindConfig.find(j.Property, {
69+
key: { name: 'darkMode' },
70+
});
71+
72+
const project = options.projectType;
73+
const newDarkModeValue = project === 'nextjs' ? 'class' : 'media';
74+
75+
if (darkModeProperty.length) {
76+
darkModeProperty.get(0).node.value = j.stringLiteral(newDarkModeValue);
77+
} else {
78+
// If darkMode property doesn't exist, add it
79+
const properties = tailwindConfig.get(0).node.right.properties;
80+
const darkModePropertyNode = j.property(
81+
'init',
82+
j.identifier('darkMode'),
83+
j.stringLiteral(newDarkModeValue)
84+
);
85+
properties.push(darkModePropertyNode);
86+
}
87+
6788
return root.toSource();
6889
} catch (err) {
6990
console.log(`\x1b[31mError: ${err as Error}\x1b[0m`);

0 commit comments

Comments
 (0)