Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
aperron committed Mar 22, 2020
2 parents 6283706 + aa148bc commit 6ab3c41
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 87 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ typings/
# This repo
tmp
dist

dist/
22 changes: 0 additions & 22 deletions .vscode/launch.json

This file was deleted.

1 change: 0 additions & 1 deletion .vscode/spellright.dict

This file was deleted.

32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,40 @@ sts path/to/strapi/api/ -o path/to/your/types/dir/

You may define multiple inputs. In case your API models have relations to other plugins like 'users-permissions'.

```console
```sh
sts path/to/strapi/api/ path/to/strapi/plugins/users-permissions/models -o path/to/your/types/dir/
```

## Enumeration
You may generate **enumeration** or **string literal** with option **-e**

```sh
sts ./api ./extensions/users-permissions/models/ -e -g ./components/ -o path/to/your/types/dir/
```

Example:
```typescript
// enumeration (with -e option)
export interface IOrder {
payment: IOrderPayment;
}

export enum IOrderPayment {
card = "card",
check = "check",
}

// OR string literal types (by default)
export interface IOrder {
payment: "card" | "check";
}
```

# Build

```console
npm i
npm run start
```sh
npm install && npm run build
# output files generated in dist folder
```

## Explanation
Expand Down
67 changes: 33 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "strapi-to-typescript",
"version": "0.3.0",
"version": "1.0.0",
"description": "Convert the strapi models to typescript interfaces.",
"main": "index.js",
"bin": {
"sts": "./bin/run.js"
},
"scripts": {
"start": "tsc -w",
"patch-release": "npm version patch && npm publish && git push --follow-tags",
"minor-release": "npm version minor && npm publish && git push --follow-tags",
"build": "tsc",
"patch-release": "npm version patch && tsc && npm publish && git push --follow-tags",
"minor-release": "npm version minor && tsc && npm publish && git push --follow-tags",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand All @@ -23,19 +24,22 @@
"generator"
],
"author": "Erik Vullings <[email protected]> (http://www.tno.nl)",
"contributors": [
"Anthony Perron <[email protected]> (http://anthonyperron.fr)"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/erikvullings/strapi-to-typescript/issues"
},
"homepage": "https://github.com/erikvullings/strapi-to-typescript#readme",
"dependencies": {
"command-line-args": "^5.0.2",
"command-line-args": "^5.1.1",
"command-line-usage": "^5.0.5"
},
"devDependencies": {
"typescript": "^2.8.3",
"@types/node": "^10.0.9",
"@types/command-line-args": "^5.0.0",
"@types/command-line-usage": "^5.0.1"
"@types/command-line-usage": "^5.0.1",
"@types/node": "^10.17.17",
"typescript": "^2.8.3"
}
}
20 changes: 20 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ const log = console.log;
export interface ICommandOptions {
/** Strapi folder(s) with models */
input: string[];
/** Strapi folder(s) with groups models */
inputGroup: string;
/** Output folder */
output: string;
/** Put all interfaces in a nested tree instead of directly under the output folder */
nested: boolean;
/** Generate enumeration */
enum: boolean;
/** Display help output */
help: boolean;
}
Expand Down Expand Up @@ -40,6 +44,14 @@ export class CommandLineInterface {
defaultOption: true,
description: 'Input folder with the Strapi models (api folder).',
},
{
name: 'inputGroup',
alias: 'g',
type: String,
typeLabel: '{underline String}',
defaultValue: undefined,
description: 'Input folder with the Strapi models (groups folder).',
},
{
name: 'output',
alias: 'o',
Expand All @@ -56,6 +68,14 @@ export class CommandLineInterface {
defaultValue: false,
description: 'If true, add each interface in its own folder.',
},
{
name: 'enum',
alias: 'e',
type: Boolean,
typeLabel: '{underline Boolean}',
defaultValue: false,
description: 'If true, Enumeration is generate, else string literal types is used',
},
];

public static sections = [
Expand Down
6 changes: 3 additions & 3 deletions src/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const walk = (
filter?: (f: string) => boolean
) => {
let foundFiles: string[] = [];
fs.readdir(dir, (err: Error, list: string[]) => {
fs.readdir(dir, (err: NodeJS.ErrnoException | null, list: string[]) => {
if (err) {
return done(err);
}
Expand Down Expand Up @@ -54,9 +54,9 @@ const walk = (
});
};

export const findFiles = (dir: string) =>
export const findFiles = (dir: string, ext: RegExp = /.settings.json$/ ) =>
new Promise<string[]>((resolve, reject) => {
const filter = (f: string) => /.settings.json$/.test(f);
const filter = (f: string) => ext.test(f);
walk(
dir,
(err, files) => {
Expand Down
4 changes: 3 additions & 1 deletion src/models/strapi-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type StrapiType = 'string' | 'number' | 'boolean' | 'text' | 'date' | 'email';
export type StrapiType = 'string' | 'number' | 'boolean' | 'text' | 'date' | 'email' | 'component' | 'enumeration';

export interface IStrapiModelAttribute {
unique?: boolean;
Expand All @@ -11,6 +11,8 @@ export interface IStrapiModelAttribute {
via?: string;
plugin?: string;
enum?: string[];
component?: string;
repeatable?: boolean;
}

export interface IStrapiModel {
Expand Down
7 changes: 5 additions & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { convert } from './ts-exporter';
import { ICommandOptions } from './cli';
import { findFilesFromMultipleDirectories, importFiles } from './importer';
import { findFilesFromMultipleDirectories, importFiles, findFiles } from './importer';

const log = console.log;
const logError = console.error;

export const exec = async (options: ICommandOptions) => {
const files = await findFilesFromMultipleDirectories(...options.input);
if(options.inputGroup){
files.push(... await findFiles(options.inputGroup, /.json/));
}
const strapiModels = await importFiles(files);
convert(options.output, strapiModels, options.nested)
convert(options.output, strapiModels, options.nested, options.enum)
.then((count) => {
log(`Generated ${count} interfaces.`);
process.exit(0);
Expand Down
Loading

0 comments on commit 6ab3c41

Please sign in to comment.