-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IM list autogenerated using scripts/generateImEnums.ts
- Loading branch information
1 parent
63d88b4
commit a852350
Showing
74 changed files
with
2,234 additions
and
129 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,66 @@ | ||
import { writeFileSync } from 'fs' | ||
import { sep } from 'path' | ||
import { config, exec, mkdir, rm } from 'shelljs' | ||
import Project, { EnumDeclarationStructure, IndentationText, QuoteKind } from 'ts-simple-ast' | ||
|
||
config.silent = true | ||
|
||
// automatically generates TS enums from all convert -list list with few exceptions that are not usable/parsable | ||
// Output folder - warning, it will be cleared | ||
const FOLDER = `src${sep}list` | ||
const EXCLUDE_FROM_LIST = ['Configure', 'Delegate', 'Magic', 'Mime', 'Threshold', 'Format', 'Coder', 'Color', 'Policy', 'Resource', 'Font', 'Locale', 'CLI', 'IMLog'] | ||
|
||
let project: Project | ||
function getProject(): Project { | ||
if (!project) { | ||
project = new Project({ | ||
useVirtualFileSystem: true, | ||
manipulationSettings: { | ||
indentationText: IndentationText.TwoSpaces, | ||
quoteKind: QuoteKind.Single, | ||
}, | ||
}) | ||
} | ||
return project | ||
} | ||
export function generateListEnum(listed: string) { | ||
const enumName = 'IM' + listed | ||
const enumDeclaration: EnumDeclarationStructure = { | ||
name: enumName, | ||
members: list(listed) | ||
.filter((v, i, a) => a.indexOf(v) === i) // deduplicate | ||
.map(n => ({ | ||
name: `'${isNaN(parseInt(n, 10)) ? n : (n + '_')}'`, | ||
initializer: `'${n}'`, | ||
})), | ||
isExported: true, | ||
} | ||
const sourceFile = getProject().createSourceFile(`${enumName}.ts`) | ||
sourceFile.addEnum(enumDeclaration) | ||
return sourceFile | ||
} | ||
|
||
export function list(listed: string): string[] { | ||
const result = exec(`convert -list ${listed}`) | ||
// ok(result.code === 0, 'result.code: ' + result.code) // commented because of https://github.com/ImageMagick/ImageMagick/issues/1333 | ||
const names = result.stdout.split('\n').filter(s => s.trim()) | ||
return names | ||
} | ||
|
||
export function generateEnumsForAllLists() { | ||
rm('-rf', FOLDER) | ||
mkdir('-p', FOLDER) | ||
let indexContent = '' | ||
list('list') | ||
.filter(name => EXCLUDE_FROM_LIST.indexOf(name) === -1) | ||
.forEach(name => { | ||
const sourceFile = generateListEnum(name) | ||
const fileName = `IM${name}` | ||
indexContent += `export * from './${fileName}'\n` | ||
writeFileSync(`${FOLDER}${sep}${fileName}.ts`, `/* auto-generated file using command \`npx ts-node scripts/generateImEnums.ts\` */\n${sourceFile.getText()}`) | ||
}) | ||
writeFileSync(`${FOLDER}${sep}index.ts`, indexContent) | ||
|
||
} | ||
|
||
generateEnumsForAllLists() |
Oops, something went wrong.