2019-05-03 20:14:57 -04:00
|
|
|
#!/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.
|
|
|
|
*/
|
|
|
|
|
2019-06-09 13:44:13 -04:00
|
|
|
const fs = require('fs-extra');
|
2019-05-03 20:14:57 -04:00
|
|
|
|
|
|
|
if (!(process.argv.length === 3 || process.argv.length === 4)) {
|
2019-06-09 13:44:13 -04:00
|
|
|
console.log('Usage: mkcomp <where?> <component-name>');
|
2019-05-03 20:14:57 -04:00
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
let name;
|
2019-06-09 13:44:13 -04:00
|
|
|
let where;
|
2019-05-03 20:14:57 -04:00
|
|
|
if (process.argv.length === 4) {
|
2019-06-09 13:44:13 -04:00
|
|
|
where = process.argv[2];
|
2019-05-03 20:14:57 -04:00
|
|
|
name = process.argv[3];
|
|
|
|
} else {
|
2019-06-09 13:44:13 -04:00
|
|
|
where = 'components';
|
2019-05-03 20:14:57 -04:00
|
|
|
name = process.argv[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!/^([a-z-])+$/.test(name)) {
|
|
|
|
console.log('must be a hyphen case name');
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:44:13 -04:00
|
|
|
const path = `./src/${where}/${name}/`;
|
2019-05-03 20:14:57 -04:00
|
|
|
fs.ensureDirSync(path);
|
|
|
|
console.log('Making path:', path);
|
|
|
|
|
2019-07-19 14:25:25 -04:00
|
|
|
const camelName = name.replace(/(^|-)[a-z]/g, s => s.replace('-', '').toUpperCase());
|
2019-06-01 20:37:54 -04:00
|
|
|
const snakeName = camelName[0].toLowerCase() + camelName.substr(1);
|
2019-05-03 20:14:57 -04:00
|
|
|
|
|
|
|
function writeFile(path, data) {
|
|
|
|
try {
|
|
|
|
return fs.writeFileSync(path, data, {
|
|
|
|
flag: 'wx', // x = fail if file exists
|
2019-07-19 14:25:25 -04:00
|
|
|
encoding: 'utf8',
|
2019-05-03 20:14:57 -04:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return console.log(`Skipping ${path}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the TypeScript file
|
2019-07-19 14:25:25 -04:00
|
|
|
writeFile(
|
|
|
|
path + name + '.tsx',
|
|
|
|
`/*
|
2019-05-03 20:14:57 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-06-09 13:44:13 -04:00
|
|
|
import React from 'react';
|
2019-05-03 20:14:57 -04:00
|
|
|
|
|
|
|
import './${name}.scss';
|
|
|
|
|
2019-06-30 22:33:16 -04:00
|
|
|
export interface ${camelName}Props {
|
2019-05-03 20:14:57 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 11:08:46 -04:00
|
|
|
export const ${camelName} = React.memo(function ${camelName}(props: ${camelName}Props) {
|
|
|
|
return (
|
|
|
|
<div className="${name}">
|
2019-06-30 22:33:16 -04:00
|
|
|
Stuff...
|
2019-10-28 11:08:46 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
2019-07-19 14:25:25 -04:00
|
|
|
`,
|
|
|
|
);
|
2019-05-03 20:14:57 -04:00
|
|
|
|
|
|
|
// Make the SASS file
|
2019-07-19 14:25:25 -04:00
|
|
|
writeFile(
|
|
|
|
path + name + '.scss',
|
|
|
|
`/*
|
2019-05-03 20:14:57 -04:00
|
|
|
* 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} {
|
|
|
|
|
|
|
|
}
|
2019-07-19 14:25:25 -04:00
|
|
|
`,
|
|
|
|
);
|
2019-06-01 20:37:54 -04:00
|
|
|
|
|
|
|
// Make the spec test file
|
2019-07-19 14:25:25 -04:00
|
|
|
writeFile(
|
|
|
|
path + name + '.spec.tsx',
|
2019-06-01 20:37:54 -04:00
|
|
|
`/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-09-11 22:42:50 -04:00
|
|
|
import { shallow } from 'enzyme';
|
2019-06-09 13:44:13 -04:00
|
|
|
import React from 'react';
|
2019-06-01 20:37:54 -04:00
|
|
|
|
|
|
|
import { ${camelName} } from './${name}';
|
|
|
|
|
2020-09-11 22:42:50 -04:00
|
|
|
describe('${camelName}', () => {
|
2019-06-09 13:44:13 -04:00
|
|
|
it('matches snapshot', () => {
|
2020-09-11 22:42:50 -04:00
|
|
|
const ${snakeName} = shallow(
|
|
|
|
<${camelName}/>
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(${snakeName}).toMatchSnapshot();
|
2019-06-01 20:37:54 -04:00
|
|
|
});
|
|
|
|
});
|
2019-07-19 14:25:25 -04:00
|
|
|
`,
|
|
|
|
);
|