Skip to content

Commit

Permalink
Add prettier format task, refactor files
Browse files Browse the repository at this point in the history
  • Loading branch information
1cedrus committed Jan 23, 2025
1 parent 962ce49 commit cf926c6
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 77 deletions.
26 changes: 26 additions & 0 deletions packages/create-typink/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
build/
build-*/
coverage/
node_modules/
tmp/
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.npmrc
.yarn/*
!.yarn/releases
!.yarn/plugins
.pnp.*
cc-test-reporter
lerna-debug.log*
npm-debug.log*
tsconfig.*buildinfo
yarn-debug.log*
yarn-error.log*
package-lock.json
.idea
.vscode
yarn.lock
LICENSE
10 changes: 10 additions & 0 deletions packages/create-typink/.prettierr.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
jsxSingleQuote: true,
printWidth: 120,
tabWidth: 2,
bracketSameLine: true,
importOrder: ['.*react.*', '^@polkadot/(.*)$', '^@dedot/(.*)$', '<THIRD_PARTY_MODULES>', '^[./]'],
};
3 changes: 2 additions & 1 deletion packages/create-typink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"chalk": "^5.4.1",
"execa": "^9.5.2",
"inquirer": "^12.3.2",
"listr2": "^8.2.5"
"listr2": "^8.2.5",
"prettier": "^3.4.2"
},
"publishConfig": {
"access": "public",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import { Listr } from 'listr2';
import { fileURLToPath } from 'url';
import * as path from 'path';
import chalk from 'chalk';
import { createFirstCommit, installPackages, createProjectDirectory, copyTemplateFiles } from '../tasks/index.js';
import {
prettierFormat,
createFirstCommit,
installPackages,
createProjectDirectory,
copyTemplateFiles,
} from '../tasks/index.js';

export async function createProject(options: Options) {
const { projectName, skipInstall, noGit } = options;

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const rootDirectory = path.resolve(__dirname, '../../');
const templateDirectory = path.resolve(__dirname, '../../templates');
const targetDirectory = path.resolve(process.cwd(), projectName!);

Expand All @@ -27,12 +34,12 @@ export async function createProject(options: Options) {
{
title: '📦 Installing dependencies with yarn, this could take a while',
task: () => installPackages(targetDirectory),
rendererOptions: {
outputBar: 8,
persistentOutput: false,
},
skip: skipInstall,
},
{
title: '🧹 Formatting the code with Prettier',
task: () => prettierFormat(rootDirectory, targetDirectory, options),
},
{
title: `🚨 Create the very first Git commit`,
task: () => createFirstCommit(targetDirectory),
Expand Down
1 change: 1 addition & 0 deletions packages/create-typink/src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './createFirstCommit.js';
export * from './copyTemplateFiles.js';
export * from './createProjectDirectory.js';
export * from './installPackages.js';
export * from './prettierFormat.js';
41 changes: 41 additions & 0 deletions packages/create-typink/src/tasks/prettierFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as prettier from 'prettier';
import * as path from 'path';
import { promises as fs } from 'fs';
import { Options } from '../types.js';
import { execa } from 'execa';

export async function prettierFormat(rootDir: string, targetDir: string, options: Options) {
if (!options.skipInstall) {
await execa('yarn', ['prettify'], { cwd: targetDir });
} else {
const prettierConfig = await prettier.resolveConfig(path.resolve(rootDir, './.prettierrc.js'));

await prettierFormatWithConfig(targetDir, prettierConfig);
}
}

export async function prettierFormatWithConfig(dir: string, config: prettier.Options | null) {
const files = await fs.readdir(dir, { withFileTypes: true });

for (const file of files) {
const filePath = path.join(dir, file.name);

if (file.isDirectory()) {
await prettierFormatWithConfig(filePath, config);
} else {
const fileInfo = await prettier.getFileInfo(filePath);

if (fileInfo.ignored || !fileInfo.inferredParser) {
continue;
}

const content = await fs.readFile(filePath, 'utf-8');
const formatted = await prettier.format(content, {
filepath: filePath,
...config,
});

await fs.writeFile(filePath, formatted, 'utf-8');
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inquirer from 'inquirer';
import arg from 'arg';
import { BaseOptions, Options, TEMPLATES, PRESET_CONTRACTS, WALLET_CONNECTORS, NETWORKS } from '../types.js';
import { IS_VALID_PACKAGE_NAME } from './string.js';

Expand Down Expand Up @@ -68,3 +69,71 @@ export async function promptMissingOptions(options: Options): Promise<Options> {
...answers,
};
}

export function parseArguments(): Options {
const args = arg(
{
'--name': String,
'-n': '--name',

'--template': String,
'-t': '--template',

'--preset': String,
'-p': '--preset',

'--wallet': String,
'-w': '--wallet',

'--network': [String],
'-N': '--network',

'--no-git': Boolean,

'--skip-install': Boolean,
'--skip': '--skip-install',

'--version': Boolean,
'-v': '--version',

'--help': Boolean,
'-h': '--help',
},
{
argv: process.argv.slice(2),
},
);

if (args['--name'] && IS_VALID_PACKAGE_NAME.test(args['--name']) === false) {
throw new Error('Project ' + args['--name'] + ' is not a valid package name. Please use a valid package name.');
}

if (args['--preset'] && !PRESET_CONTRACTS.includes(args['--preset'] as any)) {
throw new Error('Preset contract ' + args['--preset'] + ' is not supported. Please use a valid preset contract.');
}

if (args['--wallet'] && !WALLET_CONNECTORS.includes(args['--wallet'] as any)) {
throw new Error(
'Wallet connector ' + args['--wallet'] + ' is not supported. Please use a supported wallet connector.',
);
}

if (args['--network']) {
args['--network'].forEach((network: string) => {
if (!NETWORKS.includes(network as any)) {
throw new Error('Network ' + network + ' is not supported. Please use supported network.');
}
});
}

return {
projectName: args['--name'] || null,
presetContract: args['--preset'] || null,
walletConnector: args['--wallet'] || null,
network: args['--network'] || null,
skipInstall: !!args['--skip-install'],
noGit: !!args['--no-git'],
version: args['--version'] || false,
help: args['--help'] || false,
} as Options;
}
71 changes: 0 additions & 71 deletions packages/create-typink/src/utils/parseArguments.ts

This file was deleted.

1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6866,6 +6866,7 @@ __metadata:
execa: "npm:^9.5.2"
inquirer: "npm:^12.3.2"
listr2: "npm:^8.2.5"
prettier: "npm:^3.4.2"
bin:
create-typink: ./bin/create-typink.mjs
languageName: unknown
Expand Down

0 comments on commit cf926c6

Please sign in to comment.