-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add create theme command * delete jsdom * delete proxu agent * change option copy
- Loading branch information
1 parent
cf416f2
commit 336d0d7
Showing
11 changed files
with
354 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { Command } from 'commander'; | ||
import { execSync } from 'node:child_process'; | ||
import prompts, { PromptObject } from 'prompts'; | ||
import { bold, red } from 'kolorist'; | ||
import { fetchFiles } from '../../lib/fetchFilesFromGithub'; | ||
import { createTsConfigFileInFolder } from '../../lib/createTsConfigFileInFolder'; | ||
import { fileTypes } from '../../consts'; | ||
import { deleteSpecificFilesInFolder } from '../../lib/deleteSpecificFilesInFolder'; | ||
import { createFolder } from '../../lib/createFolder'; | ||
import { copyFiles } from '../../lib/copyFiles'; | ||
|
||
const extensionPrompt: PromptObject = { | ||
type: 'select', | ||
name: 'extension', | ||
message: bold('Select extension:'), | ||
choices: () => | ||
fileTypes.map(({ title, color, value }) => ({ | ||
title: color(title), | ||
value, | ||
})), | ||
}; | ||
|
||
const themeNamePrompt: PromptObject = { | ||
type: 'text', | ||
name: 'themeName', | ||
message: bold('Insert theme name:'), | ||
initial: 'my-custom-theme', | ||
}; | ||
|
||
const customDirPrompt: PromptObject = { | ||
type: 'confirm', | ||
name: 'customDir', | ||
message: bold('Choose custom directory?:'), | ||
}; | ||
const customDirPathPrompt: PromptObject = { | ||
type: 'text', | ||
name: 'customDirPath', | ||
message: bold('Insert path to custom directory:'), | ||
}; | ||
|
||
export const createTheme = new Command() | ||
.name('createTheme') | ||
.description('Creates new uniforms theme template') | ||
.option('-n, --name <name>', 'Insert theme name') | ||
.option( | ||
'-s, --skip', | ||
'skip custom directory question, and create in current directory', | ||
) | ||
.option( | ||
'-e, --extension <extension>', | ||
`Select extension (${fileTypes.map(({ value }) => value).join(', ')})`, | ||
) | ||
.action(async (options) => { | ||
const { | ||
skip: skipFlag, | ||
extension: extensionFlag, | ||
name: themeName, | ||
} = options; | ||
const findExtension = fileTypes.find( | ||
({ value }) => value === extensionFlag, | ||
); | ||
|
||
let result: prompts.Answers<'extension' | 'customDir' | 'themeName'>; | ||
try { | ||
result = await prompts( | ||
[ | ||
// @ts-expect-error | ||
...[themeName ? [] : themeNamePrompt], | ||
// @ts-expect-error | ||
...[findExtension ? [] : extensionPrompt], | ||
// @ts-expect-error | ||
...[skipFlag ? [] : customDirPrompt], | ||
], | ||
{ | ||
onCancel: (error) => { | ||
console.log('error', error); | ||
throw new Error(red('✖') + bold(' Operation cancelled')); | ||
}, | ||
}, | ||
); | ||
} catch (error: any) { | ||
console.log(error.message); | ||
return; | ||
} | ||
|
||
let customDirPath: string | undefined; | ||
let dirPathPromptResult: prompts.Answers<'customDirPath'>; | ||
if (result.customDir) { | ||
try { | ||
dirPathPromptResult = await prompts([customDirPathPrompt], { | ||
onCancel: () => { | ||
throw new Error(red('✖') + bold(' Operation cancelled')); | ||
}, | ||
}); | ||
customDirPath = dirPathPromptResult.customDirPath; | ||
} catch (error: any) { | ||
console.log(error.message); | ||
return; | ||
} | ||
} | ||
|
||
console.log('Fetching custom theme...'); | ||
const { tempDir } = await fetchFiles(); | ||
console.log('Custom theme fetched successfully.'); | ||
|
||
const extension = findExtension?.value || result.extension; | ||
|
||
const isJsx = extension === 'jsx'; | ||
|
||
if (isJsx) { | ||
console.log('Parsing files...'); | ||
createTsConfigFileInFolder(tempDir); | ||
execSync( | ||
`npm install -g typescript --quiet && cd ${tempDir} && npx --yes tsc --noCheck`, | ||
); | ||
deleteSpecificFilesInFolder(`${tempDir}/striped`, ['types.js']); | ||
console.log('Files parsed successfully.'); | ||
} | ||
|
||
console.log('Creating theme...'); | ||
const createdFolderPath = createFolder({ | ||
folderName: themeName || result.themeName, | ||
customDirPath, | ||
directory: process.cwd(), | ||
}); | ||
copyFiles(isJsx ? `${tempDir}/striped` : `${tempDir}`, createdFolderPath); | ||
console.log('Theme created successfully.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
export const copyFiles = (sourceFolder: string, destinationFolder: string) => { | ||
if (!fs.existsSync(sourceFolder)) { | ||
console.error(`Source folder not found: ${sourceFolder}`); | ||
return; | ||
} | ||
|
||
if (!fs.existsSync(destinationFolder)) { | ||
fs.mkdirSync(destinationFolder, { recursive: true }); | ||
} | ||
|
||
const files = fs.readdirSync(sourceFolder); | ||
for (const file of files) { | ||
const sourceFilePath = path.join(sourceFolder, file); | ||
const destinationFilePath = path.join(destinationFolder, file); | ||
|
||
if (fs.lstatSync(sourceFilePath).isFile()) { | ||
fs.copyFileSync(sourceFilePath, destinationFilePath); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs'; | ||
|
||
export const createFolder = ({ | ||
folderName, | ||
directory = process.cwd(), | ||
customDirPath, | ||
}: { | ||
folderName: string; | ||
directory?: string; | ||
customDirPath?: string; | ||
}) => { | ||
const dirPath = path.join( | ||
directory, | ||
customDirPath ? `/${customDirPath}` : '', | ||
folderName, | ||
); | ||
|
||
if (!fs.existsSync(dirPath)) { | ||
fs.mkdirSync(dirPath, { recursive: true }); | ||
} | ||
|
||
return dirPath; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
export const createTsConfigFileInFolder = (folderPath: string) => { | ||
const tsConfig = { | ||
include: [`./**/*`], | ||
compilerOptions: { | ||
outDir: `./striped`, | ||
target: 'es2020', | ||
module: 'es2020', | ||
strict: false, | ||
esModuleInterop: true, | ||
jsx: 'react', | ||
moduleResolution: 'node', | ||
}, | ||
}; | ||
|
||
const filePath = path.join(folderPath, 'tsconfig.json'); | ||
fs.writeFileSync(filePath, JSON.stringify(tsConfig, null, 2)); | ||
}; |
Oops, something went wrong.