loading...

2

工作中node文件操作,快速创建文件夹和文件

其他读完大概需要24分钟

  • 发布时间:2017-12-08 21:59 星期五
  • 刘伟波
  • 305
  • 更新于2017-12-18 14:33 星期一

1.工作中用到的快速创建文件夹和文件

下面代码放入新建的js文件,配置两个数组(即你要新建的文件和文件夹,可为空),用node执行即可;

const fs = require('fs');

let arr=[]; // 要新建render和templates文件的前缀
let fileArr=[]; // 要新建js和css的前缀
let _cont=['about-us','hh-oo'],out_floder='category'; // 要新建的content文件名,外层的文件夹名称

function getUpperCase(arr){
let newStr='';
for(let j in arr){
let newArr=arr[j].split('-');
for(let i in newArr){
if(i==1){
newStr+=newArr[i].toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())+',';
}else {
newStr+=newArr[i].toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())+' ';
}
}
}
let newUpperArr=newStr.split(',');
newUpperArr.splice(newUpperArr.length-1,1);
return newUpperArr;

}

let one="templatesAndRender"; // 一级目录
let two1="templates"; // 二级目录
let two2="render";
let three1="-templates"; // 三级目录
let three2="-render";
let cont=".content.xml"; // xml文件
if(arr.length!==0){
fs.mkdirSync(one);
fs.mkdirSync(one+"/"+two1);

fs.mkdirSync(one+"/"+two2);
for(let i in arr){

fs.mkdirSync(one+"/"+two1+"/"+arr[i]+three1);
fs.mkdirSync(one+"/"+two2+"/"+arr[i]+three2);
}

for(let i in arr){
let templates=`
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:description="Sony Club ${arr[i].toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())} Template."
jcr:primaryType="cq:Template"
jcr:title="Sony Club ${arr[i].toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())} Template"
ranking="{Long}100">
<jcr:content
jcr:primaryType="cq:PageContent"
sling:resourceType="sonyclub/components/render/${arr[i]}-render">
</jcr:content>
</jcr:root>
`;
fs.writeFile(one+"/"+two1+"/"+arr[i]+three1+"/"+cont, templates, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");

});


let render=`
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Component"
jcr:title="Sony Club ${arr[i].toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())} Page"
sling:resourceSuperType="sonyclub/components/structure/page"
componentGroup=".hidden"/>

`;

fs.writeFile(one+"/"+two2+"/"+arr[i]+three2+"/"+cont, templates, function(err) {
if(err) {
return console.log(err);
}
console.log(one+"/"+two2+"/"+arr[i]+three2+"/"+cont+" The file was saved!");

});

}
}
if(fileArr.length!==0){
fs.mkdirSync('clientlib');
for(let i in fileArr){
let content=`
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[sonyclub.${fileArr[i]}]"/>

`;

fs.mkdirSync('clientlib/clientlib-'+fileArr[i]);
fs.writeFileSync('clientlib/clientlib-'+fileArr[i]+'/.content.xml',content);
fs.writeFileSync('clientlib/clientlib-'+fileArr[i]+'/'+fileArr[i]+'.js','');
fs.writeFileSync('clientlib/clientlib-'+fileArr[i]+'/js.txt',fileArr[i]+'.js');
fs.writeFileSync('clientlib/clientlib-'+fileArr[i]+'/css.txt',fileArr[i]+'.css');
}
}
if(_cont.length!==0){
for(let i in _cont){
fs.mkdirSync(_cont[i]);
fs.mkdirSync(_cont[i]+'/_cq_dialog');
fs.mkdirSync(_cont[i]+'/_cq_htmlTag');
let _cont_content=`
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:nt="http://www.jcp.org/jcr/nt/1.0" jcr:primaryType="nt:unstructured"
jcr:title="Hot Recommend" sling:resourceType="cq/gui/components/authoring/dialog">
<content jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/container">
<layout jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/layouts/tabs" />
<items jcr:primaryType="nt:unstructured">
<mytabs1 jcr:primaryType="nt:unstructured" jcr:title="About I"
sling:resourceType="granite/ui/components/foundation/section">
<layout jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns" />
<items jcr:primaryType="nt:unstructured">
<tab1 jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/container">
<items jcr:primaryType="nt:unstructured">
<image jcr:primaryType="nt:unstructured"
sling:resourceType="/apps/sonystyle/components/content/common/new-pathbrowser/pathbrowser"
fieldLabel="图片" rootPath="/content/dam/sonystyle" name="./image1" />
<alt jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="图片ALT" name="./imagealt1" />
<title jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="标题" name="./title1" />
<des jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="副标题" name="./description1" />
</items>
</tab1>
</items>
</mytabs1>
<mytabs2 jcr:primaryType="nt:unstructured" jcr:title="About II"
sling:resourceType="granite/ui/components/foundation/section">
<layout jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns" />
<items jcr:primaryType="nt:unstructured">
<tab2 jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/container">
<items jcr:primaryType="nt:unstructured">
<link jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
fieldLabel="链接" rootPath="/content/sonystyle" name="./url2" />
<select jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/select"
fieldLabel="跳转方式" name="./target2">
<items jcr:primaryType="nt:unstructured">
<option1 jcr:primaryType="nt:unstructured" text="站外跳转"
value="_blank" />
<option2 jcr:primaryType="nt:unstructured" text="站内跳转"
value="_self" />
</items>
</select>
<image jcr:primaryType="nt:unstructured"
sling:resourceType="/apps/sonystyle/components/content/common/new-pathbrowser/pathbrowser"
fieldLabel="图片" rootPath="/content/dam/sonystyle" name="./image2" />
<alt jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="图片ALT" name="./imagealt2" />
<title jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="标题" name="./title2" />
<des jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="副标题" name="./description2" />
</items>
</tab2>
</items>
</mytabs2>
<mytabs3 jcr:primaryType="nt:unstructured" jcr:title="About III"
sling:resourceType="granite/ui/components/foundation/section">
<layout jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns" />
<items jcr:primaryType="nt:unstructured">
<tab3 jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/container">
<items jcr:primaryType="nt:unstructured">
<link jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
fieldLabel="链接" rootPath="/content/sonystyle" name="./url3" />
<pic jcr:primaryType="nt:unstructured"
sling:resourceType="/apps/sonystyle/components/content/common/new-pathbrowser/pathbrowser"
fieldLabel="图片" rootPath="/content/dam/sonystyle" name="./image3" />
<select jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/select"
fieldLabel="跳转方式" name="./target3">
<items jcr:primaryType="nt:unstructured">
<option1 jcr:primaryType="nt:unstructured" text="站外跳转"
value="_blank" />
<option2 jcr:primaryType="nt:unstructured" text="站内跳转"
value="_self" />
</items>
</select>
<image jcr:primaryType="nt:unstructured"
sling:resourceType="/apps/sonystyle/components/content/common/new-pathbrowser/pathbrowser"
fieldLabel="图片" rootPath="/content/dam/sonystyle" name="./image3" />
<alt jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="图片ALT" name="./imagealt3" />
<title jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="标题" name="./title3" />
<des jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
fieldLabel="副标题" name="./description3" />
</items>
</tab3>
</items>
</mytabs3>
</items>
</content>
</jcr:root>
`;
let _cont_content_out=`
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Component"
jcr:title="${out_floder.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())} - ${getUpperCase(_cont)[i]}"
componentGroup=".hidden"/>
`;
let dialog=`
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Dialog"
title="Properties"
xtype="panel">
<items jcr:primaryType="cq:WidgetCollection">
<title
jcr:primaryType="cq:Widget"
fieldLabel="Title"
name="./title"
xtype="textfield"/>
<url
jcr:primaryType="cq:Widget"
fieldLabel="URL"
name="./url"
xtype="pathfield"/>
<!-- <isDisplay
jcr:primaryType="cq:Widget"
fieldLabel="Display"
name="./isDisplay"
xtype="selection"
type="checkbox"
inputValue="false"/>-->
<isHeaderDisplay
jcr:primaryType="cq:Widget"
xtype="selection"
type="checkbox"
fieldLabel="HeaderDisplay"
name="./isHeaderDisplay"
inputValue="true"/>
<isFooterDisplay
jcr:primaryType="cq:Widget"
xtype="selection"
type="checkbox"
fieldLabel="FooterDisplay"
name="./isFooterDisplay"
inputValue="true"/>
</items>
</jcr:root>
`;
let htmlTag=`
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
cq:tagName=""
class=""
jcr:primaryType="nt:unstructured"/>
`;
fs.writeFile(_cont[i]+'/_cq_dialog/'+cont,_cont_content,function(err){
if(err) {
return console.log(err);
}
console.log(_cont[i]+'/_cq_dialog/'+cont+" The file was saved!");
});
fs.writeFile(_cont[i]+'/_cq_htmlTag/'+cont,htmlTag,function(err){
if(err) {
return console.log(err);
}
console.log(_cont[i]+'/_cq_htmlTag/'+cont+" The file was saved!");
});

fs.writeFile(_cont[i]+'/'+cont,_cont_content_out,function(err){
if(err) {
return console.log(err);
}
console.log(_cont[i]+'/'+cont+" The file was saved!");
});
fs.writeFile(_cont[i]+'/'+_cont[i]+'.html','填入html',function(err){
if(err) {
return console.log(err);
}
console.log(_cont[i]+'/'+_cont[i]+'.html'+" The file was saved!");
});
fs.writeFile(_cont[i]+'/'+'dialog.xml',dialog,function(err){
if(err) {
return console.log(err);
}
console.log(_cont[i]+'/'+'dialog.xml'+" The file was saved!");
});
}
}


你可能感兴趣的文章

    发表评论

    评论支持markdown,评论内容不能超过500字符,如果内容过多或者要及时回复,建议去 segmentfault平台, 也可以来我的直播间来提问。
    关于技术问题或者有啥不懂的都可以留言, 我会定期回复答疑, 也可以来 我的直播间 提问, 推荐最新仓库 前端知识体系, 感謝支持!