druid/web-console/script/mkcomp

175 lines
5.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs-extra');
if (!(process.argv.length === 3 || process.argv.length === 4)) {
console.log('Usage: mkcomp <where?> <component-name>');
process.exit();
}
let name;
let where;
if (process.argv.length === 4) {
where = process.argv[2];
name = process.argv[3];
} else {
where = 'components';
name = process.argv[2];
}
if (!/^([a-z-])+$/.test(name)) {
console.log('must be a hyphen case name');
process.exit();
}
const path = `./src/${where}/${name}/`;
fs.ensureDirSync(path);
console.log('Making path:', path);
const spaceName = name.replace(/-/g, ' ');
const camelName = name.replace(/(^|-)[a-z]/g, s => s.replace('-', '').toUpperCase());
const snakeName = camelName[0].toLowerCase() + camelName.substr(1);
function writeFile(path, data) {
try {
return fs.writeFileSync(path, data, {
flag: 'wx', // x = fail if file exists
encoding: 'utf8',
});
} catch (error) {
return console.log(`Skipping ${path}`);
}
}
// Make the TypeScript file
writeFile(
path + name + '.tsx',
`/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Button, InputGroup } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import classNames from 'classnames';
import React from 'react';
import './${name}.scss';
export interface ${camelName}Props {
}
export interface ${camelName}State {
}
export class ${camelName} extends React.PureComponent<${camelName}Props, ${camelName}State> {
constructor(props: ${camelName}Props, context: any) {
super(props, context);
// this.state = {};
}
render() {
return <div className="${name}">
Stuff...
</div>;
}
}
`,
);
// Make the SASS file
writeFile(
path + name + '.scss',
`/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
.${name} {
}
`,
);
// Make the spec test file
writeFile(
path + name + '.spec.tsx',
`/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { render } from 'react-testing-library';
import { ${camelName} } from './${name}';
describe('${spaceName}', () => {
it('matches snapshot', () => {
const ${snakeName} = <${camelName}
/>;
const { container } = render(${snakeName});
expect(container.firstChild).toMatchSnapshot();
});
});
`,
);