Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
printWidth: 120,
tabWidth: 2,
printWidth: 80,
endOfLine: 'lf',
trailingComma: 'none'
};
3,734 changes: 2,376 additions & 1,358 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "graphql-dev",
"version": "0.2.2",
"version": "0.3.0",
"description": "make developers effort less",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
"start": "node index.js",
"prettier": "prettier --write src/**/*.{js,ts,tsx}"
},
"bin": {
"graphql-dev": "src/index.js"
Expand All @@ -29,10 +30,29 @@
"dependencies": {
"@apollo/federation": "^0.13.2",
"apollo-datasource-rest": "^0.8.0",
"apollo-server": "^2.11.0",
"apollo-server": "^2.25.2",
"app-root-path": "^3.0.0",
"chalk": "^4.0.0",
"command-line-usage": "^6.1.0",
"figlet": "^1.3.0",
"inquirer": "^7.1.0",
"ncp": "^2.0.0",
"npm": "^6.14.4"
"npm": "^6.14.15"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,ts,tsx,json,md}": [
"prettier --write",
"git add"
]
},
"devDependencies": {
"husky": "^4.2.5",
"lint-staged": "^10.1.3",
"prettier": "^2.0.4"
}
}
196 changes: 107 additions & 89 deletions src/create-module.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const inquirer = require('inquirer');
const fs = require('fs')
const path = require('path')
const module_path = process.cwd()
const getModuleNames = module_path => fs.readdirSync(module_path).filter(f => fs.statSync(path.join(module_path, f)).isDirectory())
const fs = require('fs');
const path = require('path');
const module_path = process.cwd();
const getModuleNames = (module_path) =>
fs
.readdirSync(module_path)
.filter((f) => fs.statSync(path.join(module_path, f)).isDirectory());
let datasourcetype = 'none';
let name = '';
const fileContents = {
Expand All @@ -21,154 +24,169 @@ export class ${moduleName}DataSource extends RESTDataSource {
super();
this.baseURL = '';
}
}`
}`;
},
none: (moduleName) => {
moduleName = moduleName[0].toUpperCase() + moduleName.substring(1);
return `export class ${moduleName}DataSource { }`
return `export class ${moduleName}DataSource { }`;
}
},
typedef: `import gql from 'graphql-tag'

export const typeDefs = gql\`

\``
}
};

const createModule = async (module_path, moduleName) => {
if (module_path && moduleName) {
const moduleNameSlash = '/' + moduleName
const moduleNameSlash = '/' + moduleName;
moduleName = moduleName[0].toUpperCase() + moduleName.substring(1);
const indexContent = `
export { resolvers } from './resolvers';
export { typeDefs } from './typeDefs';
import { ${moduleName}DataSource } from './dataSource'
export const dataSources = {${moduleName}DataSource}
`
`;
const resolverContent = `export const resolvers = {
Query: {
},
Mutation: {
}
}`
}`;

const typeDefsContent = `import gql from 'graphql-tag'

export const typeDefs =
gql\`\``
gql\`\``;

const dataSourceContent = `export class ${moduleName}DataSource { }`
const dataSourceContent = `export class ${moduleName}DataSource { }`;
const path_to_folder = path.join(module_path);
module_path = path.join(path_to_folder, moduleNameSlash);
try {
fs.mkdirSync(path_to_folder + moduleNameSlash)
fs.appendFileSync(path.resolve(module_path + '/index.ts'), indexContent + "\n");
fs.appendFileSync(path.resolve(module_path + '/resolvers.ts'), resolverContent + "\n");
fs.appendFileSync(path.resolve(module_path + '/typeDefs.ts'), typeDefsContent + "\n");
fs.appendFileSync(path.resolve(module_path + '/dataSource.ts'), dataSourceContent + "\n");
fs.mkdirSync(path_to_folder + moduleNameSlash);
fs.appendFileSync(
path.resolve(module_path + '/index.ts'),
indexContent + '\n'
);
fs.appendFileSync(
path.resolve(module_path + '/resolvers.ts'),
resolverContent + '\n'
);
fs.appendFileSync(
path.resolve(module_path + '/typeDefs.ts'),
typeDefsContent + '\n'
);
fs.appendFileSync(
path.resolve(module_path + '/dataSource.ts'),
dataSourceContent + '\n'
);
} catch (err) {
console.error(`\x1b[41m'${err.message}\x1b[0m`);
}
catch (err) {
console.error(`\x1b[41m'${err.message}\x1b[0m`)
}
return true
return true;
}
}
};

const createItem = (module_path, type) => {
if (type === 'datasource') {
const source = datasourcetype === 'REST' ? 'rest' : 'none';
fs.appendFileSync(path.resolve(module_path + `/${name}.dataSource.ts`), fileContents.datasource[source](name) + "\n");
fs.appendFileSync(
path.resolve(module_path + `/${name}.dataSource.ts`),
fileContents.datasource[source](name) + '\n'
);
let data = fs.readFileSync(path.resolve(module_path + `/index.ts`));
// const fd = fs.openSync(path.resolve(module_path + `/index.ts`), 'w+');
dataname = name[0].toUpperCase() + name.substring(1);
newLine = `import {${dataname}DataSource} from './${name}.dataSource'`;
const re = new RegExp(/export const dataSources.+}/)
const re = new RegExp(/export const dataSources.+}/);
data = data.toString();
oldExport = data.match(re) ? data.match(re)[0] : '';
let newExport;
const atleastOneCharNum = new RegExp(/([a-zA-Z0-9]+)/);
if (oldExport.length && atleastOneCharNum.test(oldExport)) {
newExport = oldExport.substring(0, oldExport.length - 1) + `,${dataname}DataSource}`
data = data.replace(re, newExport)
}
else {
newExport = `\nexport const dataSources = {${dataname}DataSource}`
newExport =
oldExport.substring(0, oldExport.length - 1) +
`,${dataname}DataSource}`;
data = data.replace(re, newExport);
} else {
newExport = `\nexport const dataSources = {${dataname}DataSource}`;
data += newExport;
}
const fileContentToWrite = `${newLine}\n${data}\n`;
fs.writeFileSync(path.resolve(module_path + `/index.ts`), fileContentToWrite); //write new data
fs.writeFileSync(
path.resolve(module_path + `/index.ts`),
fileContentToWrite
); //write new data
// fs.writeSync(fd, data, 0, data.length, buffer.length); //append old data
// or fs.appendFile(fd, data);
}
else if (type === 'resolver') {
fs.appendFileSync(path.resolve(module_path + `/${name}.resolvers.ts`), fileContents.resolver + "\n");
}
else if (type === 'typedef') {
fs.appendFileSync(path.resolve(module_path + `/${name}.typeDefs.ts`), fileContents.typedef + "\n");
}
else if (type === 'module')
createModule(module_path, name)
}
} else if (type === 'resolver') {
fs.appendFileSync(
path.resolve(module_path + `/${name}.resolvers.ts`),
fileContents.resolver + '\n'
);
} else if (type === 'typedef') {
fs.appendFileSync(
path.resolve(module_path + `/${name}.typeDefs.ts`),
fileContents.typedef + '\n'
);
} else if (type === 'module') createModule(module_path, name);
};

const openDir = async (module_path, dir, type) => {

module_path = module_path + dir + "/";
const dirs = getModuleNames(module_path)
const answers = await inquirer
.prompt([
{
type: 'list',
name: 'item',
message: 'Where do you want to create?',
choices: ["confirm", ...dirs],
},
])
if (answers.item === 'confirm')
createItem(module_path, type)
else
openDir(module_path, answers.item, type)
}
module_path = module_path + dir + '/';
const dirs = getModuleNames(module_path);
const answers = await inquirer.prompt([
{
type: 'list',
name: 'item',
message: 'Where do you want to create?',
choices: ['confirm', ...dirs]
}
]);
if (answers.item === 'confirm') createItem(module_path, type);
else openDir(module_path, answers.item, type);
};

const processCreattion = async (dirs, type) => {
const readInput = await inquirer
.prompt([
{
type: 'input',
name: 'item',
message: `Give a name for your ${type}`,
choices: ["confirm", ...dirs],
},
])
name = readInput.item
const readInput = await inquirer.prompt([
{
type: 'input',
name: 'item',
message: `Give a name for your ${type}`,
choices: ['confirm', ...dirs]
}
]);
name = readInput.item;
if (type === 'datasource') {
datasourcetype = await inquirer
.prompt([
{
type: 'list',
name: 'item',
message: `Choose type of datasource`,
choices: ["REST", "none"],
},
]).then(answers => answers.item)
choices: ['REST', 'none']
}
])
.then((answers) => answers.item);
}
inquirer
.prompt([
{
type: 'list',
name: 'item',
message: `Where do yo want to create your ${type}?\nConfrim to select the folder or choose dir to open`,
choices: ["confirm", ...dirs],
},
choices: ['confirm', ...dirs]
}
])
.then(answers => {
.then((answers) => {
if (answers.item === 'confirm') {
createItem(module_path, type)
}
else {
openDir(module_path, answers.item, type)
createItem(module_path, type);
} else {
openDir(module_path, answers.item, type);
}
})
}
});
};

const newModule = () => {
inquirer
Expand All @@ -177,14 +195,14 @@ const newModule = () => {
type: 'list',
name: 'item',
message: 'What do yo wuant create?',
choices: ["module", "resolver", "datasource", "typedef"],
},
]).then(answers => {
const type = answers.item
const dirs = getModuleNames(module_path)
processCreattion(dirs, type)

})
}
choices: ['module', 'resolver', 'datasource', 'typedef']
}
])
.then((answers) => {
const type = answers.item;
const dirs = getModuleNames(module_path);
processCreattion(dirs, type);
});
};

module.exports = { newModule }
module.exports = { newModule };
Loading