Skip to content

Commit

Permalink
Merge pull request #94 from gluestack/fix/windows-support
Browse files Browse the repository at this point in the history
feat: windows support
  • Loading branch information
vaibhk20 authored Nov 5, 2024
2 parents 288788c + 60ffae3 commit 2bc7e93
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 122 deletions.
2 changes: 1 addition & 1 deletion packages/gluestack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"cli"
],
"description": "A CLI tool for easily adding components from gluestack to your projects.",
"version": "0.7.16",
"version": "0.7.16-alpha.1",
"license": "MIT",
"main": "dist/index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/gluestack-cli/src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function getProjectBasedDependencies(
}
return { dependencies: {}, devDependencies: {} };
} catch (error) {
log.error(`\x1b[31mError: ${(error as Error).message}\x1b[0m`);
throw new Error((error as Error).message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/gluestack-cli/src/util/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const componentAdder = async ({
}
if (hooksToAdd.length > 0) await hookAdder(hooksToAdd);
} catch (err) {
log.error(`\x1b[31mError: ${(err as Error).message}\x1b[0m`);
throw new Error((err as Error).message);
}
};

Expand Down
34 changes: 13 additions & 21 deletions packages/gluestack-cli/src/util/config/expo-config-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import * as path from 'path';
import fg from 'fast-glob';
import * as fs from 'fs';
import { config } from '../../config';
import { generateConfig, getFilePath } from '.';
import { _currDir, generateConfig, getFilePath, pathResolver } from '.';
import {
RawConfig,
PROJECT_SHARED_IGNORE,
ExpoResolvedConfig,
} from './config-types';
import { join, relative } from 'path';
import { execSync } from 'child_process';
import { log } from '@clack/prompts';
import { ensureFilesPromise } from '..';
import { commonInitialization } from '../init';

const _currDir = process.cwd();

// expo project type initialization
async function getExpoProjectType(cwd: string): Promise<string | undefined> {
const files = await fg.glob('**/*', {
Expand Down Expand Up @@ -68,22 +65,16 @@ async function isExpoSDK50(cwd: string): Promise<boolean> {
async function resolvedExpoPaths(resultConfig: ExpoResolvedConfig) {
const resolvedExpoPaths = {
tailwind: {
config: path.resolve(_currDir, resultConfig.tailwind.config),
css: path.resolve(_currDir, resultConfig.tailwind.css),
config: pathResolver(resultConfig.tailwind.config),
css: pathResolver(resultConfig.tailwind.css),
},
config: {
babelConfig: path.resolve(
_currDir,
resultConfig.config.babelConfig || ''
),
metroConfig: path.resolve(
_currDir,
resultConfig.config.metroConfig || ''
),
tsConfig: path.resolve(_currDir, resultConfig.config.tsConfig || ''),
babelConfig: pathResolver(resultConfig.config.babelConfig || ''),
metroConfig: pathResolver(resultConfig.config.metroConfig || ''),
tsConfig: pathResolver(resultConfig.config.tsConfig || ''),
},
app: {
entry: path.resolve(_currDir, resultConfig.app.entry || ''),
entry: pathResolver(resultConfig.app.entry || ''),
type: resultConfig?.app?.type,
sdk50: resultConfig?.app?.sdk50,
},
Expand Down Expand Up @@ -118,23 +109,24 @@ async function initNatiwindExpoApp(
expoTransformer,
'expo-add-provider-transform.ts'
);

execSync(
`npx jscodeshift -t ${metroTransformerPath} ${
resolvedConfig.config.metroConfig
} --cssPath='${cssPath}' --config='${JSON.stringify(resolvedConfig)}'`
} --cssPath=${cssPath} --isSDK50=${resolvedConfig.app.sdk50}`
);
execSync(
`npx jscodeshift -t ${BabeltransformerPath} ${resolvedConfig.config.babelConfig} --config='${JSON.stringify(resolvedConfig)}'`
`npx jscodeshift -t ${BabeltransformerPath} ${resolvedConfig.config.babelConfig} --isSDK50=${resolvedConfig.app.sdk50} --tailwindConfig=${resolvedConfig.tailwind.config}`
);
execSync(
`npx jscodeshift -t ${addProviderTransformerPath} ${resolvedConfig.app.entry} --cssImportPath='${cssImportPath}' --componentsPath='${config.writableComponentsPath}'`
`npx jscodeshift -t ${addProviderTransformerPath} ${resolvedConfig.app.entry} --cssImportPath=${cssImportPath} --componentsPath=${config.writableComponentsPath}`
);
execSync('npx expo install babel-plugin-module-resolver', {
stdio: 'inherit',
});
await commonInitialization(config.expoProject, resolvedConfig, permission);
} catch (err) {
log.error(`\x1b[31mError: ${err as Error}\x1b[0m`);
throw new Error((err as Error).message);
}
}

Expand Down Expand Up @@ -188,7 +180,7 @@ async function generateConfigExpoApp(permission: boolean) {
resolvedConfig.config.metroConfig,
resolvedConfig.config.tsConfig,
resolvedConfig.tailwind.css,
join(_currDir, 'nativewind-env.d.ts'),
pathResolver('nativewind-env.d.ts'),
];
const filesEnsured = await ensureFilesPromise(filesTobeEnsured);
if (permission && filesEnsured) {
Expand Down
28 changes: 22 additions & 6 deletions packages/gluestack-cli/src/util/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const fileExtensions = ['.tsx', '.jsx', '.ts', '.js'];
const possibleIndexFiles = ['_app', 'index', 'App'];
const possibleDirectories = ['src', 'pages', 'app', 'components'];

const _currDir = process.cwd();

const pathResolver = (p: string) => {
return path.resolve(_currDir, p).replace(/\\/g, '/');
};

function findDirectory(rootDir: string, relativePaths: string[]) {
for (const relPath of relativePaths) {
const dirPath = path.join(rootDir, relPath);
Expand Down Expand Up @@ -60,27 +66,35 @@ function getEntryPathAndComponentsPath(): {
let FileExists: string[] = [];
fileExtensions.forEach((ext) => {
possibleIndexFiles.map((file) => {
if (fs.existsSync(path.join(projectRootPath, `${file}${ext}`))) {
if (
fs.existsSync(path.join(projectRootPath, `${file}${ext}`).normalize())
) {
FileExists.push(file);
}
});
});
// Check if any of the possible index files exist
if (FileExists) {
FileExists.forEach((file) => {
entryPath.push(`./${file}.{tsx,jsx,ts,js}`);
entryPath.push(path.join('.', `${file}.{tsx,jsx,ts,js}`));
});
}
// Check if "src", "pages", "app" or "component" directories exist
possibleDirectories.forEach((dir) => {
if (fs.existsSync(path.join(projectRootPath, dir))) {
entryPath.push(`./${dir}/**/*.{tsx,jsx,ts,js}`);
if (fs.existsSync(path.join(projectRootPath, dir).normalize())) {
entryPath.push(path.join('.', `${dir}/**/*.{tsx,jsx,ts,js}`));
}
});

const resolvedPath = config.writableComponentsPath.split('/');
if (!entryPath.includes(`./${resolvedPath[0]}/**/*.{tsx,jsx,ts,js}`)) {
componentsPath.push(`./${resolvedPath[0]}/**/*.{tsx,jsx,ts,js}`);
if (
!entryPath.includes(
path.join('.', `${resolvedPath[0]}/**/*.{tsx,jsx,ts,js}`)
)
) {
componentsPath.push(
path.join('.', `${resolvedPath[0]}/**/*.{tsx,jsx,ts,js}`)
);
}
entryPath = [...entryPath, ...componentsPath];
return { entryPath };
Expand Down Expand Up @@ -130,4 +144,6 @@ export {
getComponentsPath,
generateMonoRepoConfig,
findDirectory,
pathResolver,
_currDir,
};
43 changes: 23 additions & 20 deletions packages/gluestack-cli/src/util/config/next-config-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import * as path from 'path';
import fg from 'fast-glob';
import { pathExists, readFile, writeFile } from 'fs-extra';
import { config } from '../../config';
import { findDirectory, generateConfig, getFilePath } from '.';
import {
findDirectory,
generateConfig,
getFilePath,
pathResolver,
_currDir,
} from '.';
import {
RawConfig,
NextResolvedConfig,
PROJECT_SHARED_IGNORE,
} from './config-types';
import { join, relative } from 'path';
import { execSync } from 'child_process';
import { log } from '@clack/prompts';
import { ensureFilesPromise } from '..';
import { commonInitialization } from '../init';

const _currDir = process.cwd();
//next project type initialization
async function getNextProjectType(cwd: string): Promise<string | undefined> {
const files = await fg.glob('**/*', {
Expand Down Expand Up @@ -43,21 +47,20 @@ async function getNextProjectType(cwd: string): Promise<string | undefined> {
async function resolvedNextJsPaths(resultConfig: NextResolvedConfig) {
const resolvedNextJsPaths = {
tailwind: {
config: path.resolve(_currDir, resultConfig.tailwind.config),
css: path.resolve(_currDir, resultConfig.tailwind.css),
config: pathResolver(resultConfig.tailwind.config),
css: pathResolver(resultConfig.tailwind.css),
},
config: {
postCssConfig: path.resolve(
_currDir,
resultConfig.config.postCssConfig || ''
),
nextConfig: path.resolve(_currDir, resultConfig.config.nextConfig || ''),
tsConfig: path.resolve(_currDir, resultConfig.config.tsConfig || ''),
postCssConfig: pathResolver(resultConfig.config.postCssConfig || ''),
nextConfig: pathResolver(resultConfig.config.nextConfig || ''),
tsConfig: pathResolver(resultConfig.config.tsConfig || ''),
},
app: {
entry: path.resolve(_currDir, resultConfig.app.entry || ''),
entry: pathResolver(resultConfig.app.entry || ''),
type: resultConfig?.app?.type,
registry: resultConfig?.app?.registry,
registry: resultConfig?.app?.registry
? resultConfig.app.registry.replace(/\\/g, '/')
: undefined,
page: resultConfig?.app?.page
? path.resolve(_currDir, resultConfig.app.page)
: '',
Expand All @@ -81,7 +84,7 @@ async function initNatiwindNextApp(
let nextTransformerPath = '';
let fileType = '';

if (nextConfigPath?.endsWith('.mjs')) {
if (nextConfigPath?.endsWith('.mjs') || nextConfigPath?.endsWith('.ts')) {
fileType = 'mjs';
} else if (nextConfigPath?.endsWith('.js')) {
fileType = 'js';
Expand Down Expand Up @@ -120,22 +123,21 @@ async function initNatiwindNextApp(
execSync(`npx jscodeshift -t ${transformerPath} ${docsPagePath}`);
}

const options = JSON.stringify(resolvedConfig);
const transformerPath = join(
`${NextTransformer}/next-add-provider-transform.ts --config='${options}'`
`${NextTransformer}/next-add-provider-transform.ts`
);
const rawCssPath = relative(_currDir, resolvedConfig.tailwind.css);
const cssImportPath = '@/'.concat(rawCssPath);
execSync(
`npx jscodeshift -t ${transformerPath} ${resolvedConfig.app.entry} --componentsPath='${config.writableComponentsPath}' --cssImportPath='${cssImportPath}'`
`npx jscodeshift -t ${transformerPath} ${resolvedConfig.app.entry} --componentsPath=${config.writableComponentsPath} --cssImportPath=${cssImportPath} `
);
await commonInitialization(
config.nextJsProject,
resolvedConfig,
permission
);
} catch (err) {
log.error(`\x1b[31mError: ${err as Error}\x1b[0m`);
throw new Error((err as Error).message);
}
}

Expand All @@ -153,7 +155,7 @@ async function generateConfigNextApp(permission: boolean) {
let registryPath = '';
if (projectType?.includes('app')) {
const appDirectory = findDirectory(_currDir, ['src/app', 'app']);
registryPath = path.join(_currDir, appDirectory, 'registry.tsx');
registryPath = path.resolve(_currDir, appDirectory, 'registry.tsx');
}
const pagePath = entryPath.includes('layout.')
? await getFilePath(['**/*page.*'])
Expand Down Expand Up @@ -200,7 +202,8 @@ async function generateConfigNextApp(permission: boolean) {
resolvedConfig.app.registry ?? '',
resolvedConfig.config.tsConfig,
resolvedConfig.tailwind.css,
join(_currDir, 'nativewind-env.d.ts'),
resolvedConfig.config.postCssConfig,
pathResolver('nativewind-env.d.ts'),
];
const filesEnsured = await ensureFilesPromise(filesTobeEnsured);
if (permission && filesEnsured) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import * as path from 'path';
import { generateConfig, getFilePath } from '.';
import { generateConfig, getFilePath, pathResolver } from '.';
import { RawConfig, ReactNativeResolvedConfig } from './config-types';
import { ensureFilesPromise, getRelativePath } from '..';
import { config } from '../../config';
import { join } from 'path';
import { execSync } from 'child_process';
import { log } from '@clack/prompts';
import os from 'os';
import { commonInitialization } from '../init';

const _currDir = process.cwd();

//react-native project type initialization
async function resolvedReactNativePaths(
resultConfig: ReactNativeResolvedConfig
) {
const resolvedReactNativePaths = {
tailwind: {
config: path.resolve(_currDir, resultConfig.tailwind.config),
css: path.resolve(_currDir, resultConfig.tailwind.css),
config: pathResolver(resultConfig.tailwind.config),
css: pathResolver(resultConfig.tailwind.css),
},
config: {
babelConfig: path.resolve(
_currDir,
resultConfig.config.babelConfig || ''
),
metroConfig: path.resolve(
_currDir,
resultConfig.config.metroConfig || ''
),
tsConfig: path.resolve(_currDir, resultConfig.config.tsConfig || ''),
babelConfig: pathResolver(resultConfig.config.babelConfig || ''),
metroConfig: pathResolver(resultConfig.config.metroConfig || ''),
tsConfig: pathResolver(resultConfig.config.tsConfig || ''),
},
app: {
entry: path.resolve(_currDir, resultConfig.app.entry || ''),
entry: pathResolver(resultConfig.app.entry || ''),
},
};
return resolvedReactNativePaths;
}

const podInstall = async () => {
const platform = os.platform();

if (platform === 'darwin') {
// macOS
execSync('npx pod-install', { stdio: 'inherit' });
}
};

//project specific initialization: react-native
async function initNatiwindRNApp(
resolvedConfig: ReactNativeResolvedConfig,
Expand Down Expand Up @@ -66,7 +66,7 @@ async function initNatiwindRNApp(
);

execSync(
`npx jscodeshift -t ${BabelTransformerPath} ${resolvedConfig.config.babelConfig} --config='${JSON.stringify(resolvedConfig)}'`
`npx jscodeshift -t ${BabelTransformerPath} ${resolvedConfig.config.babelConfig} --tailwindConfigPath=${resolvedConfig.tailwind.config}`
);
execSync(
`npx jscodeshift -t ${metroTransformerPath} ${resolvedConfig.config.metroConfig}`
Expand All @@ -80,9 +80,9 @@ async function initNatiwindRNApp(
permission
);

execSync('npx pod-install', { stdio: 'inherit' });
await podInstall();
} catch (err) {
log.error(`\x1b[31mError: ${err as Error}\x1b[0m`);
throw new Error((err as Error).message);
}
}

Expand All @@ -106,7 +106,7 @@ async function generateConfigRNApp(permission: boolean) {
},
app: {
entry: entryPath,
components: 'components/ui',
components: config.writableComponentsPath,
},
};
const resolvedGluestackConfig = {
Expand All @@ -122,7 +122,7 @@ async function generateConfigRNApp(permission: boolean) {
tsConfig: tsConfigPath.length ? tsConfigPath : 'tsconfig.json',
},
app: {
entry: path.resolve(_currDir, entryPath),
entry: pathResolver(entryPath),
},
};

Expand All @@ -135,7 +135,7 @@ async function generateConfigRNApp(permission: boolean) {
resolvedConfig.config.metroConfig,
resolvedConfig.config.tsConfig,
resolvedConfig.tailwind.css,
join(_currDir, 'nativewind-env.d.ts'),
pathResolver('nativewind-env.d.ts'),
];
const filesEnsured = await ensureFilesPromise(filesTobeEnsured);
if (permission && filesEnsured) {
Expand Down
Loading

0 comments on commit 2bc7e93

Please sign in to comment.