-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create theme command #1
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
})), | ||
}; | ||
piotrpospiech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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?:'), | ||
piotrpospiech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would reconfigure
eslint
. Most of plugins are unnecessary, for exampleeslint-plugin-react
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i will do it in next pr, also add husky and some sort of ci to test lints on branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, but don't focus on
husky
. Configure CI first. Git hooks can be controversial topic.